Cmultifileupload is used for uploading files and uploading multiple files at once. This UI component is based on the jquery Multi File Upload plugin. Many of the UI components built into Yii are based on jquery, so you need to create a assets directory to hold dynamically generated javascripts.
The information of the uploaded file can be accessed via $_files[widget-name], for example, the file information uploaded by Cmultifileupload's name "files" can be accessed by $_files [' Files ']. Also, the form property containing cmultifileupload needs to be set enctype=multipart/form-data.
This example creates a upload directory to hold the uploaded file. We import the directory through the configuration file settings in the upload file.
Modify/config/main.php Add Project code
Application-level parameters that can be accessed//using Yii::app ()->params[' paramname '] ' params ' =>require ( DirName (__file__). ' /params.php '),
Add some parameters for application, the file that holds the parameter is config/param.php
The directory that defines the upload file is as follows:
This contains the application parameters that can bemaintained via Guireturn array (//upload directory ' Uploaddir ' = ' upload/',);
This parameter can be accessed in code via Yii::app ()->params[' Uploaddir '), and for this simple example you can also use upload/as a fixed constant without defining the application parameter params.
This example does not need to use model, we define the view as follows:
Beginwidget (' Cactiveform ', Array (' method ' = ' post ', ' Htmloptions ' =>array (' enctype ' = ' multipart/') Form-data '));? >widget (' Cmultifileupload ', Array (' name ' = ' files ', ' accept ' = ' jpg|png ', ' Max ' =>3, ' Remove ' = ' and ' remove ',//' denied ' = ', ', message that's displayed when a file type is not allowed//' duplicate ' = = ', mes Sage that was displayed when a file appears twice ' htmloptions ' =>array (' size ' =>25),));? >endwidget ();?> FindFiles () as $filename):? >yii::app ()->baseurl. ' /'. Yii::app ()->params[' Uploaddir ']. $filename, Array (' rel ' = ' external '));? >
Using Cmultifileupload to upload a file with the extension jpg|png, Cmultifileupload can define some of the options through the configuration, specifically reference
Modify its corresponding controller/action.
Class Sitecontroller extends ccontroller{/*** Index action is the default action in a Controller.*/public function Actioni Ndex () {if (Isset ($_files[' FILES ')) {//delete old Filesforeach ($this->findfiles () as $filename) unlink (Yii::app ()- >params[' Uploaddir ']. $filename)//upload new Filesforeach ($_files[' FILES ' [' name '] as $key = $filename) move_ Uploaded_file ($_files[' FILES '] [' tmp_name '] [$key],yii::app ()->params[' Uploaddir ']. $filename);} $this->render (' Index ');} /*** @return Array filename*/public function findfiles () {return Array_diff (Scandir (Yii::app ()->params[' Uploaddir ' ]), array ('. ', '.. '));}}
The action method first deletes the file under the upload directory and then stores the uploaded file in that directory.
The above is the content of the PHP Development Framework Yii Framework Tutorial (MULTIFILEUPLOAD) UI component sample, more about topic.alibabacloud.com (www.php.cn)!