Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move_uploaded_file() Unable to move file from tmp to dir

I've been searching for the solution but I can't find the answer.

I created a image upload form. It runs with ajaxform plugin. But still it doesn't upload to the directory. The error_log says

move_uploaded_file() Unable to move file from [tmp] to [dir].

Then on the front end it says Upload Complete. But when the file is called, it doesn't exist.

HTML CODE:

<div class="imgupload hidden">  
    <form id="profpicform" action="" method="post" enctype="multipart/form-data">
        <input type="file" size="60" name="profpic">
        <input type="submit" value="Submit File">
    </form>

    <div class="imguploadStatus">
        <div class="imguploadProgress">
            <div class="imguploadProgressBar"></div>
            <div class="imguploadProgressPercent">0%</div>
        </div>
        <div class="imguploadMsg"></div>
    </div>
    <div class="imgpreview"></div>
</div>

JS:

var options = { 
    beforeSend: function() 
    {
        $(".imguploadProgress").show();
        $(".imguploadProgressBar").width('0%');
        $(".imguploadMsg").html("");
        $(".imguploadProgressPercent").html("0%");
    },
    uploadProgress: function(event, position, total, percentComplete) 
    {
        $(".imguploadProgressBar").width(percentComplete+'%');
        $(".imguploadProgressPercent").html(percentComplete+'%');

    },
    success: function() 
    {
        $(".imguploadProgressBar").width('100%');
        $(".imguploadProgressPercent").html('100%');

    },
    complete: function(response) 
    {
        alert('Complecion');
    },
    error: function()
    {
        $(".imguploadMsg").html("<font color='red'> ERROR: unable to upload files</font>");
    }

    }; 

     $("#profpicform").ajaxForm(options);

SEVER SIDE:

$output_dir = home_url()."/path/to/dir/";

if(isset($_FILES["profpic"])){
    if ($_FILES["profpic"]["error"] > 0){
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }else{
        move_uploaded_file($_FILES["profpic"]["tmp_name"],$output_dir. $_FILES["profpic"]["name"]);
        if(get_user_meta($_SESSION['userid'], 'user_profile_picture')==""){
            add_user_meta($_SESSION['userid'], 'user_profile_picture', $_FILES['profpic']);
        }else{
            update_user_meta($_SESSION['userid'], 'user_profile_picture', $_FILES['profpic']);
        }
        echo "Uploaded File :".$_FILES["profpic"]["name"];
    }
}

They are only found in one PHP file. Folder permission for the directory is 777.

like image 437
Wadapmerth Avatar asked Jan 10 '14 02:01

Wadapmerth


1 Answers

Look into the below case:

$file_tmp = $_FILES['file']['tmp_name'];
$file_name = $_FILES['file']['name'];
$file_destination = 'upload/' . $file_name;
move_uploaded_file($file_tmp, $file_destination);

In the above code snippet, I have used $_FILES array with the name of the input field that is “file” to get the name and temporary name of the uploaded file.

Check the scenario that cause the error:

Make sure you have created folder where you want to move your file.
Check for the folder permission, is it writable or not. Use chmod() to modify permission.
See if your target path URL is properly created or not. You can use dirname(__FILE__) to get the absolute path. To get current working directory, use getcwd().
like image 143
Sara Vaseei Avatar answered Sep 20 '22 13:09

Sara Vaseei