php - move_uploaded_file() 无法将文件从 tmp 移动到 dir

标签 php file upload ajaxform

我一直在寻找解决方案,但找不到答案。

我创建了一个图片上传表单。它与 ajaxform 插件一起运行。但它仍然没有上传到目录。 error_log 说

move_uploaded_file() Unable to move file from [tmp] to [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。

最佳答案

试试这个:

$destination_path = getcwd().DIRECTORY_SEPARATOR;
$target_path = $destination_path . basename( $_FILES["profpic"]["name"]);
@move_uploaded_file($_FILES['profpic']['tmp_name'], $target_path)

关于php - move_uploaded_file() 无法将文件从 tmp 移动到 dir,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21035320/

相关文章:

php - 叠加在单个图标上

javascript - Ajax读取PHP生成的json

java - 在 Java 中搜索文件

python - 为什么我在图片上添加评论后图片的尺寸会变小?

php - 上传图片到主机

javascript - 如何使用 jQuery 获取重复表单中的字段值?

php - 这个 PHP 库中的 '\' 是什么意思?

c - 文件 c 中的多个结构

javascript - 如何使用 javascript 将多个文件上传到 firebase 存储引用?

ios - 无法使用 Xcode 7.1 上传到 App Store