StreetCoder
StreetCoder

Reputation: 10061

Uploadify gives error HTTP Error (302) in yii

Yesterday I integrated uploadify in a Yii application and it worked good. But suddenly I found that it does not work rather showing error HTTP Error (302). I found this is happened only firefox, IE and Crome are okay.

In below of main.php file I wrote:

<?php $timestamp = time();?>
$(function() {
    $('#file_upload').uploadify({
        'method'   : 'post',
        'auto'     :false,
        'multi'    : true,
        'formData'     : {
            'timestamp' : '<?php echo $timestamp;?>',
            'token'     : '<?php echo md5('unique_salt' . $timestamp);?>'
        },
        'swf'      : '<?php echo Yii::app()->request->baseUrl; ?>/images/uploadify.swf',
        'uploader' : '<?php echo Yii::app()->createUrl('cp/project/UploadImage')?>'

    });
});

and in controller:

public function actionUploadImage(){
    $directory = Yii::getPathOfAlias('webroot').'/images/temp';
    if (!is_dir($directory)) {
        mkdir($directory, 0777, true);
    }

    $verifyToken = md5('unique_salt' . $_POST['timestamp']);

    if (!empty($_FILES) && $_POST['token'] == $verifyToken) {

        $tempFile = $_FILES['Filedata']['tmp_name'];

        $filename  = basename($_FILES['Filedata']['name']);
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
        $newName       = md5(time()).'.'.$extension;

        $targetFile = $directory . '/' . $newName;

        $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
        $fileParts = pathinfo($_FILES['Filedata']['name']);

        if (move_uploaded_file($tempFile,$targetFile))
        {
            $model = new ImageTemp();
            $model->user_id = Yii::app()->user->id;
            $model->image_hash = $newName;
            $model->session_id = Yii::app()->session['imageUpload'];
            $model->save();
        }

    }
}

Everything was good but suddenly, I have been facing this error.

Upvotes: 1

Views: 3230

Answers (1)

StreetCoder
StreetCoder

Reputation: 10061

I have solved the issue and left answer here how I have fixed if anybody got any help. As per reference of uploadify http://www.uploadify.com/documentation/uploadify/using-sessions-with-uploadify/ and http://www.uploadify.com/documentation/uploadify/customizing-the-server-side-upload-script/ I wrote at the beginning of the controller :

function init(){

    if(isset($_POST['SESSION_ID'])){
        $session=Yii::app()->getSession();
        $session->close();
        $session->sessionID = $_POST['SESSION_ID'];
        $session->open();
    }
}

and initialized uploadify as:

$session_id = Yii::app()->session->sessionID;
$session_name = 'SESSION_ID';
$swf = Yii::app()->request->baseUrl.'/images/uploadify.swf';
$uploadifyImg = Yii::app()->createUrl('cp/project/UploadImage');
Yii::app()->clientScript->registerScript('uploadify', "

$(function() {
    $('#file_upload').uploadify({
        'method'   : 'post',
        'auto'     :false,
        'multi'    : true,
        'formData' : {'$session_name' : '$session_id'},
        'swf'      : '$swf',
        'uploader' : '$uploadifyImg'

    });
  });
");

It would helpful who face such issue.

Upvotes: 0

Related Questions