Home  >  Article  >  PHP Framework  >  Use the yii framework to implement file upload and download functions

Use the yii framework to implement file upload and download functions

王林
王林forward
2020-08-17 17:04:013227browse

Use the yii framework to implement file upload and download functions

1. CUploadedFile implements single file upload

(Recommended tutorial: yii framework)

(1) First, in the model The class declares an attribute to store the file name (either form model or active record model). Also declare a file validation rule to ensure that uploaded files have the specified extension.

class Item extends CActiveRecord
{
    public $image;
    // ... other attributes
 
    public function rules()
    {
        return array(
            array('image', 'file', 'types'=>'jpg, gif, png'),
        );
    }
}

(2) Then, define an action method in the controller class to collect the data submitted by the user

class ItemController extends CController
{
    public function actionCreate()
    {
        $model=new Item;
        if(isset($_POST['Item']))
        {
            $model->attributes=$_POST['Item'];
            $model->image=CUploadedFile::getInstance($model,'image');
            if($model->save())
            {
                $model->image->saveAs('path/to/localFile');
                // redirect to success page
            }
        }
        $this->render('create', array('model'=>$model));
    }
}

(recommended related tutorials: php graphic tutorial)

(3) Finally, create the action view and generate an uploaded field.

<?php echo CHtml::form(&#39;&#39;,&#39;post&#39;,array(&#39;enctype&#39;=>&#39;multipart/form-data&#39;)); ?>
...
<?php echo CHtml::activeFileField($model, &#39;image&#39;); ?>
...
<?php echo CHtml::endForm(); ?>

2. CUploadedFile implements multiple file uploads

Method: view view code:

for($i=0;$i<3;$i++){
    //echo $form->fileField($model,&#39;xiangguan_tupian[]&#39;).&#39;<br/>&#39;;//这种方法不行
    //echo CHtml::activeFileField($model,&#39;xiangguan_tupian[]&#39;);//这种也不行
    //echo CHtml::fileField(&#39;xiangguan_tupian[]&#39;,&#39;&#39;,array(&#39;id&#39;=>&#39;xiangguan_tupian&#39;.$i)).&#39;&nbsp&#39;;//这种也不行
    echo CHtml::activeFileField($model,&#39;xiangguan_tupian[]&#39;,array(&#39;id&#39;=>&#39;xiangguan_tupian&#39;.$i));
}
?>

controller controller side:                                                                                                   The upload is processed.

(Learning video recommendation:

php video tutorial

) Key points:

$model=new Info;
// echo &#39;<hr><br>&#39;;
$obj_array=CUploadedFIle::getInstances($model,&#39;xiangguan_tupian&#39;);
//注意这里不是getInstance而是getInstances多了个s,这样得到的是一个包含CUploadedFile对象的数组
//print_r($obj_array);
foreach($obj_array as $k=>$v){
    $v->saveAs(Yii::app()->basePath.&#39;/&#39;.$k.&#39;_test.&#39;.$v->getExtensionName());
}

3. Use sendFile() to download files

CUploadedFile::getInstance();    // 返回的是一个CUploadedFile对象,
CUploadeFile::getInstanceByName();   //返回的是一个CUploadedFile对象            
CUploadedFile::getInstances()    //返回的是一个值为CUploadedFile对象的数组
CUploadedFile::getInstancesByName();   //返回的是一个值为CUploadedFile对象的数组

The above is the detailed content of Use the yii framework to implement file upload and download functions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete
Previous article:Is yii open source?Next article:Is yii open source?