PHP開發架構Yii Framework教程(15) UI 組件 MultiFileUpload樣本

來源:互聯網
上載者:User

CMultiFileUpload 用於上傳檔案,支援一次同時上傳多個檔案。這個UI組件是基於jQuery Multi File Upload 外掛程式。Yii 內建的很多UI組件都是基於JQuery,因此需要建立assets 目錄用於存放動態產生的javascripts 等。

所上傳的檔案的信 息可以通過$_FILES[widget-name]來訪問,比如,CMultiFileUpload的name為”files” 所上傳的檔案資訊可以通過$_FILES ['files']來訪問。此外包含CMultiFileUpload的Form屬性需要設定enctype=multipart/form-data。

本例建立 一個upload目錄用於存放上傳的檔案。我們通過設定檔設定中個上傳檔案匯入目錄。

修改/config/main.php 添加項目 代碼

// application-level parameters that can be accessed     // using Yii::app()->params['paramName']     'params'=>require(dirname(__FILE__).'/params.php'),

為Application添加一些參數,存放參數的檔案為 config/param.php

定義上傳檔案的目錄如下:

// this contains the application parameters that can be maintained via GUI     return array(              //upload directory     'uploadDir' => 'upload/',     );

在代碼中可以通過Yii::app()->params['uploadDir'] 來訪問這個參數,對於這個簡單的例子,你也可 以直接使用upload/ 做為固定的常量而無需定義Application的參數params.

本例不需使用Model,我們定義View如下:

<center class="form">     <?php $form=$this->beginWidget('CActiveForm',array(                  'method' =>'post',         'htmlOptions'=>array('enctype'=>'multipart/form-data'),                  )); ?>                 <?php $this->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),        )); ?>                  <center class="row submit">             <?php echo CHtml::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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.