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

时间:2014-01-10 02:45:56

标签: php file upload ajaxform

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

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

  

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

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

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"];
    }
}

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

3 个答案:

答案 0 :(得分:8)

试试这个:

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

答案 1 :(得分:1)

调查以下情况:

var productsThatCanBeDelivered = dbContext.Products.Select(product => new
{
    // Select only the product properties that you plan to use:
    Id = product.Id,
    AvailableQuantity = product.AvailableQuantity,

    ConfirmedOrders = product.Orders
        .Where(order => order.Confirmed)
        .Select(order => new
        {
            // select only the order properties that you plan to use
            Id = order.Id,
            Quantity = order.Quantity,
            ...

            // no need for this: you already know the value
            // ProductId = order.ProductId,
            // Confirmed = order.Confirmed,
        })
        .ToList(),
    })

    // keep only those Products where the quantities of the confirmed orders is less than ...
    .Where(product => product.ConfirmedOrders
                             .Select(confiredOrder => confirmedOrder.Quantity)
                             .Sum() < product.AvailableQuantity);

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

检查导致错误的情况:

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

链接:https://www.dospeedtest.com/blogs/php-warning-move_uploaded_file-unable-to-move/

答案 2 :(得分:0)

$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/':是您要保存文件的目录。