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.
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().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With