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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21035320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:45:21  来源:igfitidea点击:

move_uploaded_file() Unable to move file from tmp to dir

phpfileuploadajaxform

提问by Wadapmerth

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

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

move_uploaded_file() Unable to move file from [tmp] to [dir].

move_uploaded_file() 无法将文件从 [tmp] 移动到 [dir]。

Then on the front end it says Upload Complete. But when the file is called, it doesn't exist.

然后在前端它说上传完成。但是当文件被调用时,它不存在。

HTML CODE:

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:

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.

它们只能在一个 PHP 文件中找到。该目录的文件夹权限为 777。

回答by Zword

Try this:

尝试这个:

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

回答by Piyush

$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.
  • getcwd(): 从根获取当前工作目录路径。
  • 'images/': 是您要保存文件的目录。