PHP development framework YiiFramework tutorial (15) UI component MultiFileUpload example

Source: Internet
Author: User
CMultiFileUpload is used to upload files. multiple files can be uploaded at a time. This UI component is based on the jQueryMultiFileUpload plug-in. Many built-in UI components of Yii are based on JQuery. Therefore, you need to create an assets directory to store dynamically generated javascripts. CMultiFileUpload is used to upload files. multiple files can be uploaded at a time. This UI component is based on the jQuery Multi File Upload plug-in. Many built-in UI components of Yii are based on JQuery. Therefore, you need to create an assets directory to store dynamically generated javascripts.

The information of the uploaded file can be accessed through $ _ FILES [widget-name]. for example, the file information uploaded by CMultiFileUpload whose name is "files" can be accessed through $ _ FILES ['Files. In addition, enctype = multipart/Form-data must be set for the form attribute that contains CMultiFileUpload.

In this example, create an upload directory to store uploaded files. We use the configuration file to set the import Directory of the uploaded file.

Modify/config/main. php to 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 the Application. the file storing the parameters is config/param. php.

The directory that defines the file to be uploaded is as follows:

// this contains the application parameters that can bemaintained via GUIreturn array(//upload directory'uploadDir' => 'upload/',);

In the code, you can access this parameter through Yii: app ()-> params ['uploaddir']. for this simple example, you can also directly use upload/as a fixed constant without defining the Application parameter params.

In this example, you do 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'=>'Remove',//'denied'=>'', message that is displayed when a file type is not allowed//'duplicate'=>'', message that is 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'));?>

Use CMultiFileUpload to upload files with the extension jpg | png. you can define some options by configuring CMultiFileUpload. for details, see

Modify the corresponding Controller/Action.

class SiteController extends CController{/*** Index action is the default action in a controller.*/public function actionIndex(){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 files under the upload directory, and then stores the uploaded files in this directory.

The above is the content of the MultiFileUpload sample UI component in the PHP development Framework Yii Framework tutorial (15). For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.