国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

Yii框架上傳圖片用法總結

2024-05-04 23:44:29
字體:
來源:轉載
供稿:網(wǎng)友
這篇文章主要介紹了Yii框架上傳圖片用法,結合實例形式總結分析了Yii框架上傳圖片的相關注意事項與使用技巧,需要的朋友可以參考下
 

本文實例講述了Yii框架上傳圖片用法。分享給大家供大家參考,具體如下:

Yii 提供了 CUploadedFile 來上傳文件,比如圖片,或者文檔。

官方關于這個類的介紹 :

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
實現(xiàn)上傳文件,要用到MVC三個層面。

 

1、 模型層面 M ,把一個字段在rules方法里設置為 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,上傳失敗!請上傳小于10M的文件!'),

2、視圖層View,這里需要用到CHtml::activeFileField 來生成選擇文件的button,注意是上傳文件,所以在該標單中enctype應該設置為: 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');  //獲得一個CUploadedFile的實例  if(is_object($file)&&get_class($file) === 'CUploadedFile'){  // 判斷實例化是否成功    $model->url = './assets/upfile/file_'.time().'_'.rand(0,9999).'.'.$file->extensionName;  //定義文件保存的名稱  }else{    $model->url = './assets/upfile/noPic.jpg';    // 若果失敗則應該是什么圖片  }  if($model->save()){    if(is_object($file)&&get_class($file) === 'CUploadedFile'){      $file->saveAs($model->url); // 上傳圖片    }    $this->redirect(array('view','id'=>$model->lid));  }}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 中卫市| 梨树县| 河北省| 大渡口区| 龙山县| 老河口市| 鹤山市| 泰兴市| 北碚区| 应城市| 清远市| 武威市| 延津县| 眉山市| 黑水县| 乌兰察布市| 通州区| 白朗县| 沙坪坝区| 江西省| 莆田市| 辉县市| 尼玛县| 阜阳市| 策勒县| 兰西县| 清河县| 芜湖县| 方城县| 简阳市| 上思县| 焦作市| 会昌县| 小金县| 芜湖市| 独山县| 曲沃县| 桐柏县| 莫力| 闽侯县| 大丰市|