Eajaxupload Debuging

Hi,

In order to upload a image file, using ajax, I’m using an extension called eajaxupload. It seems very easy to use but hard to debug.

Every time I upload a file, the only thing happening is a print of the file name and ‘Failed’, next to that.

No server errors.

The php.ini has upload_max_filesize = 10M and post_max_size = 10M.

The folder permissions is 777.

This is how that is in the view:


<?

    $this->widget('ext.EAjaxUpload.EAjaxUpload', array(

        'id' => 'uploadFile',

        'config' => array(

            'action' => Yii::app()->createUrl('mestre/upload'),

            'allowedExtensions' => array("jpg", "png", "gif", "jpeg"), //array("jpg","jpeg","gif","exe","mov" and etc...

            'sizeLimit' => 1 * 1024 * 1024, // maximum file size in bytes

            'minSizeLimit' => 1, // minimum file size in bytes

            'onComplete' => "js:function(id, fileName, responseJSON){ alert(responseJSON); }",

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

        )

    ));

    ?>

And this is how that is in the controller:


Yii::import("ext.EAjaxUpload.qqFileUploader");        

        $allowedExtensions = array("jpg", "jpeg", "gif", "png"); //array("jpg","jpeg","gif","exe","mov" and etc...

        $sizeLimit = 1 * 1024 * 1024; // maximum file size in bytes

        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);

        $result = $uploader->handleUpload($folder);

        $result = htmlspecialchars(json_encode($result), ENT_NOQUOTES);

        $foto_name = $result['filename'];

        $this->_foto_name = $foto_name;



Can some one see the bug here? How should I debug this?

Thanks

The size limit in the Widget configuration says 1 * 1024 * 1024, which is only 1 MB. Do you need to increase that?

No, the file is smaller than that.

Finally I get the error message: “Server error. Upload directory isn’t writable”.

I’ve changed the folder permissions to 777, and I steel have that error. After that I change the destination folder to my desktop folder, and despite the file is uploaded correctly, the message ‘failed’ is printed again and I get a server error “Illegal string offset ‘filename’” when I’m trying to get the file name this way:




 $allowedExtensions = array("jpg", "jpeg", "gif", "png"); //array("jpg","jpeg","gif","exe","mov" and etc...

        $sizeLimit = 1 * 1024 * 1024; // maximum file size in bytes

        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);

        $result = $uploader->handleUpload($folder);

        echo '<script> console.log("result = ' . print_r($result) . '") </script>';

        $result = htmlspecialchars(json_encode($result), ENT_NOQUOTES);


        $foto_name = $result['filename'];



Once more, Thanks Kieth!

You’ve assigned the output of htmlspecialchars() to $result and then treated $result as an array. htmlspecialchars() returns a string.

If the folder is world writable, you shouldn’t have any trouble saving the image there. Make sure that the folder you’re writing to is specified correctly.

Once more, you’re right!

So, how can I access to the file name?

Keith, you save my day…

The documentation uses slightly confusing variable names. Note the difference between assigning to $result and $return.




$return = htmlspecialchars(json_encode($result), ENT_NOQUOTES);



Then you’re not overwriting $result and can continue to treat it as an array.

Keith, every thing it’s working like a charm, thanks for your patience. but tell me just one more thing: how can i get rid of the ‘failed’ message printed after the upload?

Thanks

I’m not sure what’s causing the ‘failed’ message to display. You might have to look into the source code of the library to determine the problem.