zk546113096 2011-05-17 14:24:43 17634次浏览 34条回复 0 0 0

Agile Web Application Development with Yii 1.1 and PHP5I regularly used SWFUpload in my projects to handle multiple file uploads, most of them picture gallery related functions. Just because I am using Yii now I wasn’t going to change that, so I was ready to build my SWFUpload extension in order to continue with my ‘traditional’ way of doing things. One of the lessons learned along these years of programming is not to reinvent the wheel and first thing I did was searching the Yii extensions repository just to check if anybody has done something like that before; and yes, somebody did: mr ThiagoVidal. That made things easier :)

I made small modifications to the extension as I didn’t like the handlers.js file (is part of the SWFUpload package) to be outside of the extension’s package. I thought that a plug and play extension should be all in one place, so it is easier to use and move around projects. So, for the sake of this example, and if mr ThiagoVidal haven’t updated my modifications, please download the zipped package below. Installation

Once you download the files, unzip the contents of the package and place all in your applications protected/widgets folder. How to use

First of all, we need to tweak our index.php file. This is because SWFUpload requirements with a variable named PHPSESSID that is needed for handling file uploads. Due that is a conditional statement I decided to put it in the main file as won’t affect at all the rest of the application.

if (isset($_POST['PHPSESSID']))
{
    $_COOKIE['PHPSESSID'] = $_POST['PHPSESSID'];
}

Now, we are going to construct a view that will handle the uploads. The following example is extracted from one of my projects that most of the CMS calls are done through AJAX and this view is actually displayed via fancybox . In our view, we first configure the SWFUpload widget and afterwards its HTML like this:

SWFUpload

//'use_query_string'=>true,
// Creating the URL that will handle our uploads
// check the parameter at the end
// $uurl = $this->createUrl('picture/upload',array('yourvarname'=>$yourvar));
$uurl = $this->createUrl('picture/upload');
// here is our widget!
$this->widget('widgets.CSwfUpload', array(
// here you include the post params you want the
// upload function to handle when a file is submitted
//  'postParams'=>array('yourvarname'=>$yourvar),
'config'=>array(
    'use_query_string'=>false,
    //Use $this->createUrl method or define yourself
    'upload_url'=> CHtml::normalizeUrl($uurl),
   // This is a workaround to avoid certain
   // issues (check SWFUpload Forums)
   'file_size_limit'=>'2 gb',
   // Allowed file types
   'file_types'=>'*.jpg;*.png;*.gif',
   // File types description (mine spanish)
   'file_types_description'=>'Imagenes',
   // unlimited number of files
   'file_upload_limit'=>0,
   // refer to handlers.js from here below
   'file_queue_error_handler'=>'js:fileQueueError',
   'file_dialog_complete_handler'=>'js:fileDialogComplete',
   'upload_progress_handler'=>'js:uploadProgress',
   'upload_error_handler'=>'js:uploadError',
   'upload_success_handler'=>'js:uploadSuccess',
   'upload_complete_handler'=>'js:uploadComplete',
   // what is our upload target layer?
   'custom_settings'=>array('upload_target'=>'divFileProgressContainer'),
   // where are we going to place the button?
   'button_placeholder_id'=>'swfupload',
   'button_width'=>230,
   'button_height'=>20,
   'button_text'=>'<span class="button">Selecciona las Imagenes (2 MB Max)</span>',
   'button_text_style'=>'.button { font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif; font-size: 11pt; text-align: center; }',
   'button_text_top_padding'=>0,
   'button_text_left_padding'=>0,
   'button_window_mode'=>'js:SWFUpload.WINDOW_MODE.TRANSPARENT',
   'button_cursor'=>'js:SWFUpload.CURSOR.HAND',
   ),
  )
);

HTML

<!-- SPANISH sorry -->
<div id="main-content">
<h2>Cargador de Im&aacute;genes</h2>
<p>Haz click en el siguiente boton para cargar las im&aacute;genes de la Propiedad.
El cargador de Im&aacute;genes las cargar&aacute; y redimensionar&aacute; autom&aacute;ticamente.
</p>
<form>
<div class="form">
    <div class="row">
    <div class="swfupload"  style="display: inline; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;">
       <span id="swfupload"></span>
    </div>
    <div id="divFileProgressContainer" style="height: 75px;"></div>
    <div id="thumbnails"></div>
    </div>
</div>
</form>
</div>

Ok, now that we have our view (and placed in its correspondent folder within your application’s protected/view folder), we need to have a controller that handles the file uploads of SWFUpload and the rendering of the view. I have one controller named picture that renders the view on an uploader action and handles the uploads through its upload action. One thing very important is that we remove accessControl’s filters for our Upload action, otherwise our code just won’t work at all. Please, remember to set the rules() of your controller for each action respectively, I have them set only accessible by the administrator in my CMS. Here is the code:

//
// lets remove access control filters
// for our upload action
public function filters()
{
   return array(
     'accessControl -upload',
    );
}
public function actionUploader(){
// In case you wish to  post a variable each time a file
// is uploaded. This is good if a picture is related to an object
// and you wish to pass the ID so to related both picture
// or file to parent.
// $yourvar = 'variabletopost';
// $this->render('uploader',array('yourvar'=>$yourvar));
   $this->render('uploader');
   // In my case is AJAX
   Yii::app()->end();
}
// For the sake of simplicity the following function
// does not have any file validation procedures
// I highly recommend you that you create one
// If you need help, let me know, and I will forward
// mine so you see an example.
public function actionUpload(){
 try{
     // get the file
     $picture_file = CUploadedFile::getInstanceByName('Filedata');
     if(!$picture_file || $picture_file->getHasError()){
    echo 'Error: Documento Invalido';
    Yii::app()->end();
     }
     // remember the post params?
     // $yourvar = Yii::app()->request->getParam('yourvarname');
     $picture_name = $picture_file->name;
     //
     // I normally here I use PhpThumb Library instead of this
     // make sure 'thepathyousavethefile' is a writable path
     $picture_file->saveAs(
         Yii::getPathOfAlias('thepathyousavethefile').
         $picture_name
      );
      // Return the file id to the script
      // This will display the thumbnail of the uploaded file to the view
      echo "FILEID:" . Yii::app()->getBaseUrl() .
         '/thepathyousavethepicture/' .
         $picture_name;
     }
     catch(Exception $e){
    echo 'Error: ' . $e->getMessage();
     }
     Yii::app()->end();
}

Final Words

I would like to remark that the upload action I created above is very simple and some file validations should be applied such as checking their sizes, file types, and so on. I used this approach without problems at all.

[attach]150[/attach]

  • 回复于 2011-05-17 14:28 举报

    哈哈哈,多谢啊!

  • 回复于 2011-05-17 14:37 举报

    顶和尚!

  • 回复于 2011-05-17 14:41 举报

    谢谢,我现在做的项目就需要SWFUpload上传

  • 回复于 2011-05-18 00:48 举报

    谢谢的话就免了,继续顶起来啊

  • 回复于 2011-05-19 05:20 举报

    这个swfupload困扰了我很长时间了

  • 回复于 2011-05-19 09:29 举报

    那看完这个,解决了没啊

  • 回复于 2011-05-28 02:44 举报

    晕了啊,不让下载,还扣掉我5分。

  • 回复于 2011-05-28 10:06 举报

    你钱不够还是咋样,问问舰长去吧

  • 回复于 2011-05-28 10:08 举报

    你把情况描述清楚可以吗?

  • 回复于 2011-05-28 14:28 举报

    是这样的,新注册一个用户,做完论坛的任务之后,获得50分,成为注册用户,这时可以下载文件了。

    当你下载一个文件的时候,系统会扣掉你5分,扣掉之后,本来就应该发送下载的文件的,但系统在发送之前,又检查了一次用户的身份。而此时用户由于被扣掉5分,身份就不再是注册用户了。所以系统就停止发送文件,提示没有下载权利。但是5分已经被系统白白扣掉了。

    这是一个逻辑上的错误。不知道是不是故意这么写的。

    如果要避免这个问题,只需要把检查身份的步骤移到扣分之前即可。

  • 回复于 2011-05-28 16:18 举报

    这还得问舰长啊,你应该回复他的,他是坛主。。

  • 回复于 2011-05-29 09:46 举报

    我就是报告一个bug,改就改了,实惠以后来的人,不改也无所谓。

  • 回复于 2011-06-24 09:25 举报

    貌似现在扣10分啊。。。。。

  • 回复于 2011-06-24 09:26 举报

    还有就是这个是直接copy官网上的吗??

  • 回复于 2011-06-24 09:41 举报

    是对的。。。。。。。

  • 回复于 2011-07-18 15:41 举报

    问啥是英文的捏。。。

  • 回复于 2011-07-19 09:30 举报

    英文好啊。。。。。。

  • 回复于 2011-07-19 10:50 举报

    上传中文件名的文件时会出现乱码 并且找不到文件

  • 回复于 2011-07-19 20:20 举报

    [attach]247[/attach]这个西班牙人写的版本有点问题,我把修改过的版本发一下,主要是viewr的代码里的不一样,cotroller里的代码还是一样的,还有就是完善了那个功能。
    view里的代码

    <?php 
    //'use_query_string'=>true,
    // Creating the URL that will handle our uploads
    // check the parameter at the end
    // $uurl = $this->createUrl('picture/upload',array('yourvarname'=>$yourvar));
    $uurl = $this->createUrl('site/upload');
    // here is our widget!
    $this->widget('CSwfUpload', array(
    // here you include the post params you want the
    // upload function to handle when a file is submitted
    //  'postParams'=>array('yourvarname'=>$yourvar),
    'config'=>array(
        'use_query_string'=>false,
        //Use $this->createUrl method or define yourself
        'upload_url'=> CHtml::normalizeUrl($uurl),
       // This is a workaround to avoid certain
       // issues (check SWFUpload Forums)
       'file_size_limit'=>'2 gb',
       // Allowed file types
       'file_types'=>'*.jpg;*.png;*.gif',
       // File types description (mine spanish)
       'file_types_description'=>'Imagenes',
       // unlimited number of files
       'file_upload_limit'=>0,
       // refer to handlers.js from here below
       'file_queued_handler'=>'js:fileQueued',
       'file_queue_error_handler'=>'js:fileQueueError',
       'file_dialog_complete_handler'=>'js:fileDialogComplete',
       'upload_progress_handler'=>'js:uploadProgress',
       'upload_error_handler'=>'js:uploadError',
       'upload_success_handler'=>'js:uploadSuccess',
       'upload_complete_handler'=>'js:uploadComplete',
       // what is our upload target layer?
       'custom_settings'=>array(
            'upload_target'=>'divFileProgressContainer',
            'cancelButtonId'=>"btnCancel",
            'progressTarget'=>"fsUploadProgress",),
       // where are we going to place the button?
       'button_placeholder_id'=>'swfupload',
       'button_width'=>230,
       'button_height'=>20,
       'button_text'=>'<span class="button">Selecciona las Imagenes (2 MB Max)</span>',
       'button_text_style'=>'.button { font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif; font-size: 11pt; text-align: center; }',
       'button_text_top_padding'=>0,
       'button_text_left_padding'=>0,
       'button_window_mode'=>'js:SWFUpload.WINDOW_MODE.TRANSPARENT',
       'button_cursor'=>'js:SWFUpload.CURSOR.HAND',
       ),
      )
    );
    
    ?>
    
    <div id="main-content">
    
    <h2>Cargador de Im&aacute;genes</h2>
    
    <p>Haz click en el siguiente boton para cargar las im&aacute;genes de la Propiedad.
    
    El cargador de Im&aacute;genes las cargar&aacute; y redimensionar&aacute; autom&aacute;ticamente.
    
    </p>
    
    <form>
    
    <div class="form">
    
        <div class="row">
         <div  id="fsUploadProgress">
                                <span class="legend">上传队列</span>
         </div>
    
        <div class="swfupload"  style="display: inline; border: solid 1px #7FAAFF; background-color: #C5D9FF; padding: 2px;">
    
           <span id="swfupload"></span>
    
        </div>
    
        <div id="divFileProgressContainer" style="height: 75px;"></div>
    
        <div id="thumbnails"></div>
    
        </div>
    
    </div>
    <input class="btn_startupload" type="button" onclick="swfu.startUpload();" value="开始上传">
    </form>
    
    </div>
    
  • 回复于 2011-07-20 09:11 举报

    落叶V5。。。。。。

您需要登录后才可以回复。登录 | 立即注册