Yii框架上傳圖片用法總結(jié)
本文實(shí)例講述了Yii框架上傳圖片用法。分享給大家供大家參考,具體如下:
Yii 提供了 CUploadedFile 來(lái)上傳文件,比如圖片,或者文檔。
官方關(guān)于這個(gè)類的介紹 :
CUploadedFile represents the information for an uploaded file.
Call getInstance to retrieve the instance of an uploaded file, and then use saveAs to save it on the server. You may also query other information about the file, including name, tempName, type, size and error.
public properties
Property | Type | Description | Defined By |
---|---|---|---|
error | integer | Returns an error code describing the status of this file uploading. | CUploadedFile |
extensionName | string | the file extension name for name. | CUploadedFile |
hasError | boolean | whether there is an error with the uploaded file. | CUploadedFile |
name | string | the original name of the file being uploaded | CUploadedFile |
size | integer | the actual size of the uploaded file in bytes | CUploadedFile |
tempName | string | the path of the uploaded file on the server. | CUploadedFile |
type | string | the MIME-type of the uploaded file (such as "image/gif"). | CUploadedFile |
1、 模型層面 M ,把一個(gè)字段在rules方法里設(shè)置為 file 屬性。
array('url', 'file', //定義為file類型 'allowEmpty'=>true, 'types'=>'jpg,png,gif,doc,docx,pdf,xls,xlsx,zip,rar,ppt,pptx', //上傳文件的類型 'maxSize'=>1024*1024*10, //上傳大小限制,注意不是php.ini中的上傳文件大小 'tooLarge'=>'文件大于10M,上傳失??!請(qǐng)上傳小于10M的文件!' ),
2、視圖層View,這里需要用到CHtml::activeFileField 來(lái)生成選擇文件的button,注意是上傳文件,所以在該標(biāo)單中enctype應(yīng)該設(shè)置為: multupart/form-data
<?php $form=$this->beginWidget('CActiveForm', array( <span style="white-space:pre"> </span>'id'=>'link-form', <span style="white-space:pre"> </span>'enableAjaxValidation'=>false, <span style="white-space:pre"> </span>'htmlOptions' => array('enctype'=>'multipart/form-data'), )); ?>
<div class="row"> <?php echo $form->labelEx($model,'url'); ?> <?php echo CHtml::activeFileField($model,'url'); ?> <?php echo $form->error($model,'url'); ?> </div>
3、控制層 C
$model=new Link; if(isset($_POST['Link'])) { $model->attributes=$_POST['Link']; if(empty($_POST['Link']['name'])){ $model->name = $model->url; } $file = CUploadedFile::getInstance($model,'url'); //獲得一個(gè)CUploadedFile的實(shí)例 if(is_object($file)&&get_class($file) === 'CUploadedFile'){ // 判斷實(shí)例化是否成功 $model->url = './assets/upfile/file_'.time().'_'.rand(0,9999).'.'.$file->extensionName; //定義文件保存的名稱 }else{ $model->url = './assets/upfile/noPic.jpg'; // 若果失敗則應(yīng)該是什么圖片 } if($model->save()){ if(is_object($file)&&get_class($file) === 'CUploadedFile'){ $file->saveAs($model->url); // 上傳圖片 } $this->redirect(array('view','id'=>$model->lid)); } }
更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開(kāi)發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所幫助。
- YII中Ueditor富文本編輯器文件和圖片上傳的配置圖文教程
- yii2高級(jí)應(yīng)用之自定義組件實(shí)現(xiàn)全局使用圖片上傳功能的方法
- yii2整合百度編輯器umeditor及umeditor圖片上傳問(wèn)題的解決辦法
- yii實(shí)現(xiàn)圖片上傳及縮略圖生成的方法
- Yii結(jié)合CKEditor實(shí)現(xiàn)圖片上傳功能
- yii2利用自帶UploadedFile實(shí)現(xiàn)上傳圖片的示例
- Yii+upload實(shí)現(xiàn)AJAX上傳圖片的方法
- Yii2實(shí)現(xiàn)ajax上傳圖片插件用法
- yii使用activeFileField控件實(shí)現(xiàn)上傳文件與圖片的方法
- yii上傳文件或圖片實(shí)例
- Yii框架實(shí)現(xiàn)圖片上傳的方法詳解
相關(guān)文章
淺談php中的循環(huán)while、do...while、for、foreach四種循環(huán)
下面小編就為大家?guī)?lái)一篇淺談php中的循環(huán)while、do...while、for、foreach四種循環(huán)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11Yii視圖操作之自定義分頁(yè)實(shí)現(xiàn)方法
這篇文章主要介紹了Yii視圖操作之自定義分頁(yè)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Yii自定義分頁(yè)的實(shí)現(xiàn)步驟與相關(guān)技巧,需要的朋友可以參考下2016-07-07搭建PhpStorm+PhpStudy開(kāi)發(fā)環(huán)境的超詳細(xì)教程
這篇文章主要介紹了搭建PhpStorm+PhpStudy開(kāi)發(fā)環(huán)境的超詳細(xì)教程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09ThinkPHP表單數(shù)據(jù)智能寫入create方法實(shí)例分析
這篇文章主要介紹了ThinkPHP表單數(shù)據(jù)智能寫入create方法,以實(shí)例形式較為詳細(xì)的分析了ThinkPHP中create只能寫入的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09Yii框架參數(shù)化查詢中IN查詢只能查詢一個(gè)的解決方法
這篇文章主要介紹了Yii框架參數(shù)化查詢中IN查詢只能查詢一個(gè)的解決方法,結(jié)合實(shí)例形式分析了Yii框架中IN查詢只能查一個(gè)的原因及FIND_IN_SET函數(shù)相關(guān)功能與使用技巧,需要的朋友可以參考下2017-05-05淺析Yii2 GridView實(shí)現(xiàn)下拉搜索教程
這篇文章主要介紹了淺析Yii2 GridView實(shí)現(xiàn)下拉搜索教程的相關(guān)資料,需要的朋友可以參考下2016-04-04