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 bemaintained via GUIreturn array(//upload directory'uploadDir' => 'upload/',);

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

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

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'));?>

使用CMultiFileUpload上傳副檔名為jpg|png 的檔案,CMultiFileUpload可以通過配置定義一些選項,具體可以參考

修改其對應的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('.', '..'));}}

Action方法首先刪除upload目錄下的檔案,然後將上傳的檔案存放到該目錄下。

以上就是PHP開發架構Yii Framework教程(15) UI 組件 MultiFileUpload樣本的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.