Yii will not move_uploaded_file - 500 internal server error

1.2k views Asked by At

I'm using the blueimp File Upload Plugin with Yii to try and upload a file to my server (currently localhost). I gave the folder full read / write permissions (the location is C:\xampp\htdocs\yii), but I still get an error when I try to do the move_uploaded file command.

Here is the main form and input file area:

<form id='upload' method='post' action='?r=site/move' enctype='multipart/form-data' style="padding:0;">
    <span class="btn fileinput-button" style="padding:0">
        <i class="glyphicon glyphicon-picture">
            <input id="fileupload" type="file" accept="image/*" name="attachment" onchange="attachAttachment()">
        </i>                                        
    </span>
</form>

Here is blueimp's fileupload (in function()):

$("#fileupload").fileupload
({

        dataType: 'json', 

        done: function (e, data) 
        {
            console.log("YAY");
        },

        fail: function(e, data)
        {
            console.log("FAIL");
        }
});

Here is the actionMove, where I move the file from the temp directory to the folder:

public function actionMove()
{
    if (isset($_FILES['attachment']) && $_FILES['attachment']['error'] == 0)
    {           
        if (move_uploaded_file($_FILES['attachment'], Yii::getPathOfAlias('webroot')."/images/uploads")){ 
            $response = '{"status":"success"}';
        }
        else { 
            $response = '{"status":"error"}';
        }

        echo $response; 
        exit();
    }   
}

I have been at this for hours now, any help is appreciated :(

1

There are 1 answers

0
Mat On BEST ANSWER

$_FILES['attachment'] references all data about the download. move_uploaded_file uses filenames to work. Here is what you should try:

$uploadPath = Yii::getPathOfAlias('webroot')."/images/uploads";
$uploadFilename = basename($_FILES['attachment']['name']);
$tempFilename = $_FILES['attachment']['tmp_name'];
$ok = move_uploaded_file($tempFilename, $uploadPath.'/'.$uploadFilename);
if ($ok) {
    $response = '{"status":"success"}';
} else {
    $response = '{"status":"error"}';
}

More on this on the documentation pages: http://php.net/manual/fr/features.file-upload.post-method.php and http://php.net/manual/fr/function.move-uploaded-file.php

Hope it helps.