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

Posted

技术标签:

【中文标题】move_uploaded_file() 无法将文件从 tmp 移动到 dir【英文标题】:move_uploaded_file() Unable to move file from tmp to dir 【发布时间】:2014-01-28 21:29:00 【问题描述】:

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

我创建了一个图片上传表单。它与 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。

【问题讨论】:

【参考方案1】:

试试这个:

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

【讨论】:

谢谢老兄!只需轻轻一点,就能完美运行! 请您深入解释一下【参考方案2】:
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$target_path = $destination_path . 'images/'. basename( $_FILES["profpic"]["name"]);
move_uploaded_file($_FILES['profpic']['tmp_name'], $target_path);
getcwd() : 从根目录获取当前工作目录路径。 'images/' :是您要保存文件的目录。

【讨论】:

【参考方案3】:

查看以下案例:

$file_tmp = $_FILES['file']['tmp_name'];
$file_name = $_FILES['file']['name'];
$file_destination = 'upload/' . $file_name;
move_uploaded_file($file_tmp, $file_destination);

在上面的代码sn-p中,我使用了$_FILES数组,输入字段的名称为“文件”,来获取上传文件的名称和临时名称。

检查导致错误的场景:

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().

【讨论】:

以上是关于move_uploaded_file() 无法将文件从 tmp 移动到 dir的主要内容,如果未能解决你的问题,请参考以下文章

move_uploaded_file 无法打开流:权限被拒绝 - Mac

PHP/IIS copy()/move_uploaded_file() 无法打开流:权限被拒绝警告

move_uploaded_file 无法打开流和权限被拒绝错误

PHP move_uploaded_file无法在公共服务器上运行

无法将文本区域值传递给我的表单

Ubuntu Server不会使用PHP“move_uploaded_file”上传文件