Search code examples
cakephpuploadifycakephp-2.1

Uploadify doing action run twice


I'm using the jquery uploadify in CakePHP, and I'm having problems with the $ _SESSION, since I have the action below that receives the user id by $ this-> request-> params ['named'] ['uid'] the problem is that with the uploadify enabled action is executed twice, but the second data $ this-> request-> params ['named'] ['uid'] are lost.

Following is the code simplified for better understanding.

FilesController.php / index **running twice

public function index() {
        $id = $this->request->params['named']['uid'];
        $us = $this->User->findById($id)['User'];
        $dir = UPLOAD_DIR . $us['id'] . '/';
        CakeSession::write('Files.atual', $dir);
        $this->set('u', $us);
        //$this->autoRender = false;
}

Views/Elements/up.ctp //Enabling Uploadify

<?php $timestamp = time(); ?>
    $(function() {
        $('#file_upload').uploadify({
            'formData'     : {
                'timestamp' : '<?php echo $timestamp; ?>',
                'token'     : '<?php echo md5('unique_salt' . $timestamp); ?>'
            },
            'swf'      : '<?= $this->request->webroot; ?>uploadify.swf',
            'preventCaching' : false,
            'uploader' : '<?= $this->request->webroot; ?>uploadify.php?session_id=<?php echo(session_id()); ?>',
            'buttonClass' : 'button icon-paperclip',
            'buttonText' : 'Enviar Arquivos',
            'onUploadSuccess' : function(file, data, response) {
                $('.files-icons').load('<?= $this->request->webroot; ?>files/reload/<?= $u['id'] ?>');
            }
        });
    });

Solution

  • The double request to your upload action is caused by a bug in the button_background_url property of uploadify. You can set your own background image which stops the additional request being made to your action by setting this in your .uploadify configuration:

    'button_image_url': '/img/cake.icon.png',
    

    Just as an example. You might want to set it to null or some other background image.