PHP Development Framework Yii Framework Tutorial (a) UI component Multifileupload sample

Source: Internet
Author: User
Tags config file upload php foreach yii

Cmultifileupload is used to upload files and supports uploading multiple files at once. This UI component is based on the jquery Multi File Upload plug-in. Many of the UI components built into Yii are based on jquery, so you need to create a assets directory to store dynamically generated javascripts.

The uploaded file information can be accessed through $_files[widget-name], for example, the file information uploaded by Cmultifileupload's name "files" can be accessed by $_files [' Files ']. In addition, the form property containing the cmultifileupload needs to be set enctype=multipart/form-data.

This example creates a upload directory to store uploaded files. We import the directory through the profile settings in the upload file.

Modify/config/main.php Add Project code

Application-level parameters that can is accessed     
//Using Yii::app ()->params[' paramname '] ' params ' =>     
Require (DirName (__file__). /params.php '),

Add some parameters to the application, and the file that holds the parameters is config/param.php

The directory that defines the uploaded files is as follows:

This contains the application parameters the can is 

maintained via GUI return     
Array (     
         
//upload directory
   ' Uploaddir ' => ' upload/',     
);

You can access this parameter in code by Yii::app ()->params[' Uploaddir ', and for this simple example, you can use upload/as a fixed constant without having to define application parameter params.

This example does not need to use model, we define view as follows:

<center class= "Form" > <?php $form = $this->beginwidget (' Cactiveform ', Array (' method ' =& gt; '     
         
   Post ', ' Htmloptions ' =>array (' enctype ' => ' multipart/form-data '));?>     
    <?php $this->widget (' Cmultifileupload ', Array (' name ' => ' files ', ' Accept ' => ' jpg|png '),  ' Max ' =>3, ' remove ' => ' remove ',//' denied ' => ', message This is displayed when a file type is Not allowed//' duplicate ' => ', the message this is displayed when a file appears twice ' Htmloptions ' =&gt ; Array (' Size ' =>25));?> <center class= "row Submit" > <?php echo C Html::submitbutton (' Upload ');?> </center> <?php $this->endwidget ();?>      
    ;/center><!--form--> <ul> <?php foreach ($this->findfiles () as $filename):?> <li><?php Echo Chtml::link ($filename, Yii::app ()->baseurl. /'. Yii::app ()->params[' Uploaddir ']. $filename, Array (' rel ' => ' external '));? ></li> <?php Endforeach;?> </ul>

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.