我一直在寻找解决方案,但找不到答案。
我创建了一个图像上传表单。它与 ajaxform 插件一起运行。但它仍然没有上传到目录。
error_log 说
move_uploaded_file() 无法将文件从 [tmp] 移动到 [dir]。
然后在前端显示上传完成。但是当调用该文件时,它并不存在。
HTML 代码:
<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);
服务器端:
$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"];
}
}
它们只能在一个 PHP 文件中找到。该目录的文件夹权限为777。