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

Use the yii framework to implement file upload and download functions

王林
Release: 2020-08-17 17:04:01
forward
3268 people have browsed it

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'),
        );
    }
}
Copy after login

(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));
    }
}
Copy after login

(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(); ?>
Copy after login

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));
}
?>
Copy after login

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());
}
Copy after login

3. Use sendFile() to download files

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

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template