Question

I am new to Yii and want to integrate file uploading.Unfortunately I have not been able to make it work.I have downloaded and extracted the plugin to /extensions/ folder but now I do not know what to do.

Can some one guide me?

here is the link for extension

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

Was it helpful?

Solution

that extension is outdated and poorly supported. I use this extension instead: eajaxupload.

As you can see on that page the usage is extremely simple. Upload it to your extensions dir and install it with these few lines of code

First you put the following code in your controller action:

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
}

After that go to the associated view file for that action and use this code to generate the upload form:

<? $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); }"
              )
)); ?>

Extremely easy to get working. Give it a go!

OTHER TIPS

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'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top