如何使用Yii的xupload

时间:2011-12-17 09:46:14

标签: php yii yii-extensions yii-xupload

我是Yii的新手并希望整合文件上传。遗憾的是我无法使其正常工作。我已将插件下载并解压缩到/ extensions /文件夹但现在我不知道该怎么做。

有人可以指导我吗?

这是扩展名

的链接

http://www.yiiframework.com/extension/xupload/

2 个答案:

答案 0 :(得分:4)

该扩展程序已过时且支持不足。我改为使用此扩展程序:eajaxupload

正如您在该页面上看到的那样,使用非常简单。将其上传到您的扩展程序目录,并使用这几行代码

进行安装

首先,将以下代码放入控制器操作中:

public function actionUpload()
{
        Yii::import("ext.EAjaxUpload.qqFileUploader");

        $folder='upload/';// folder for uploaded files
        $allowedExtensions = array("jpg");//array("jpg","jpeg","gif","exe","mov" and etc...
        $sizeLimit = 10 * 1024 * 1024;// maximum file size in bytes
        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
        $result = $uploader->handleUpload($folder);
        $result=htmlspecialchars(json_encode($result), ENT_NOQUOTES);

        $fileSize=filesize($folder.$result['filename']);//GETTING FILE SIZE
        $fileName=$result['filename'];//GETTING FILE NAME

        echo $result;// it's array
}

之后转到该操作的关联视图文件,并使用此代码生成上传表单:

<? $this->widget('ext.EAjaxUpload.EAjaxUpload',
array(
        'id'=>'uploadFile',
        'config'=>array(
               'action'=>'/controller/upload',
               'allowedExtensions'=>array("jpg");//array("jpg","jpeg","gif","exe","mov" and etc...
               'sizeLimit'=>10*1024*1024,// maximum file size in bytes
               'minSizeLimit'=>10*1024*1024,// minimum file size in bytes
               //'onComplete'=>"js:function(id, fileName, responseJSON){ alert(fileName); }",
               //'messages'=>array(
               //                  'typeError'=>"{file} has invalid extension. Only {extensions} are allowed.",
               //                  'sizeError'=>"{file} is too large, maximum file size is {sizeLimit}.",
               //                  'minSizeError'=>"{file} is too small, minimum file size is {minSizeLimit}.",
               //                  'emptyError'=>"{file} is empty, please select files again without it.",
               //                  'onLeave'=>"The files are being uploaded, if you leave now the upload will be cancelled."
               //                 ),
               //'showMessage'=>"js:function(message){ alert(message); }"
              )
)); ?>

非常容易上班。试一试!

答案 1 :(得分:-1)

To change the filename before saving the image, uncomment the commented line and provide the name of your choice.

./extensions/EAjaxUpload/qqFileUploader.php

    $pathinfo = pathinfo($this->file->getName());
    $filename = $pathinfo['filename'];
    //$filename = md5(uniqid());
    $ext = $pathinfo['extension'];