Skip to content
Advertisement

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.

Advertisement

Answer

Try this:

$destination_path = getcwd().DIRECTORY_SEPARATOR;
$target_path = $destination_path . basename( $_FILES["profpic"]["name"]);
@move_uploaded_file($_FILES['profpic']['tmp_name'], $target_path)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement