move_uploaded_file() Unable to move file from tmp to dir
Asked Answered
S

3

17

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.

Seamy answered 10/1, 2014 at 2:45 Comment(0)
M
18

Try this:

$destination_path = getcwd().DIRECTORY_SEPARATOR;
$target_path = $destination_path . basename( $_FILES["profpic"]["name"]);
@move_uploaded_file($_FILES['profpic']['tmp_name'], $target_path)
Mosaic answered 10/1, 2014 at 2:48 Comment(2)
Thanks dude! With little twerking, worked perfectly!Seamy
would you please explain in deepSpeedwriting
H
3

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().
Hundley answered 10/6, 2020 at 10:15 Comment(0)
K
-2
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$target_path = $destination_path . 'images/'. basename( $_FILES["profpic"]["name"]);
move_uploaded_file($_FILES['profpic']['tmp_name'], $target_path);
  • getcwd() : getting current working directory path from the root.
  • 'images/' : is the directory where you want to save your file.
Keister answered 13/9, 2019 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.