《PHP學(xué)習(xí):Yii框架上傳圖片用法總結(jié)》要點(diǎn):
本文介紹了PHP學(xué)習(xí):Yii框架上傳圖片用法總結(jié),希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
相關(guān)主題:YII框架
PHP實(shí)例本文實(shí)例講述了Yii框架上傳圖片用法.分享給大家供大家參考,具體如下:
PHP實(shí)例Yii 提供了 CUploadedFile 來上傳文件,比如圖片,或者文檔.
PHP實(shí)例官方關(guān)于這個(gè)類的介紹 :
PHP實(shí)例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 |
PHP實(shí)例1、 模型層面 M ,把一個(gè)字段在rules辦法里設(shè)置為 file 屬性.
PHP實(shí)例
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的文件!'
),
PHP實(shí)例2、視圖層View,這里需要用到CHtml::activeFileField 來生成選擇文件的button,注意是上傳文件,所以在該標(biāo)單中enctype應(yīng)該設(shè)置為: multupart/form-data
PHP實(shí)例
<?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'),
)); ?>
PHP實(shí)例
<div class="row">
<?php echo $form->labelEx($model,'url'); ?>
<?php echo CHtml::activeFileField($model,'url'); ?>
<?php echo $form->error($model,'url'); ?>
</div>
PHP實(shí)例3、控制層 C
PHP實(shí)例
$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));
}
}
PHP實(shí)例更多關(guān)于Yii相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結(jié)》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
PHP實(shí)例希望本文所述對(duì)大家基于Yii框架的PHP程序設(shè)計(jì)有所贊助.
歡迎參與《PHP學(xué)習(xí):Yii框架上傳圖片用法總結(jié)》討論,分享您的想法,維易PHP學(xué)院為您提供專業(yè)教程。
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/7224.html