文件上传没有将文件移动到tmp文件夹

时间:2016-11-02 23:31:15

标签: php html5

我有以下html表格:



<form enctype="multipart/form-data" method="post" action="image_upload_script.php?u=<?php echo $u; ?>">
<div id="dropzone">
    <div>Drop Files Here Or Click To Browse</div>
    <input name="upload" id="upload" class="button" type="file" />
</div>
<input type="submit" id="uplbtn" value="Upload"/>
</form>
&#13;
&#13;
&#13;

这是相关的php部分:

&#13;
&#13;
$fileName = $_FILES["upload"]["name"]; 
$u = $_GET['u'];
$fileTmpLoc = $_FILES["upload"]["tmp_name"];
$fileType = $_FILES["upload"]["type"]; 
$fileSize = $_FILES["upload"]["size"]; 
$fileErrorMsg = $_FILES["upload"]["error"]; 
$kaboom = explode(".", $fileName); 
$fileExt = end($kaboom); 

print_r($_FILES);
if (!$fileTmpLoc) { 
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
} else if($fileSize > 5242880) { 
    echo "ERROR: Your file was larger than 5 Megabytes in size.";
    unlink($fileTmpLoc); 
    exit();
} else if (!preg_match("/.(gif|jpg|png)$/i", $fileName) ) {
     echo "ERROR: Your image was not .gif, .jpg, or .png.";
     unlink($fileTmpLoc); 
     exit();
} else if ($fileErrorMsg == 1) { 
    echo "ERROR: An error occured while processing the file. Try again.";
    exit();
}
$moveResult = move_uploaded_file($fileTmpLoc, "/var/www/domain.com/public_html/static_domain/upload/Images/$fileName");
if ($moveResult != true) {
	$error = error_get_last();
    echo "ERROR: $error[message]";
    unlink($fileTmpLoc); 
    exit();
}
&#13;
&#13;
&#13;

这提供了以下来自php的反馈:

  

数组([upload] =&gt;数组([名称] =&gt; 1961 Impala工厂照片3a.jpg   [type] =&gt; image / jpeg [tmp_name] =&gt; / tmp / phpSOhr6N [error] =&gt; 0 [大小]   =&GT; 46396))错误:move_uploaded_file():无法移动&#39; / tmp / phpSOhr6N&#39;至   &#39; /var/www/domain.com/public_html/static_domain/upload/Images/1961   Impala工厂照片3a.jpg&#39;

它只是没有将它移动到tmp - 我通过在php中注释unlink来测试它,看看它是否仍然会坐在那里,如果我检查,但没有这样的运气:( 所有涉及的文件夹都有0777权限,www-data是每个文件夹的所有者,tmp在1777完全免费。当表单文件和php文件存在于我的同一个文件夹中时,它也会显示此行为https-static域以及没有https的子域(现在)。我在这做错了什么?我忘记了什么吗?非常感谢您的帮助。谢谢!

2 个答案:

答案 0 :(得分:2)

尝试更改

$moveResult = move_uploaded_file($fileTmpLoc, "/var/www/domain.com/public_html/static_domain/upload/Images/$fileName");

$moveResult = move_uploaded_file($fileTmpLoc, $fileName);

查看它是否将其移动到当前文件夹中。如果它不会进入当前文件夹,请尝试相对路径(假设您的image_upload_script.php位于public_html / static_domain中)

$moveResult = move_uploaded_file($fileTmpLoc, 'upload/Images/'.$fileName);

<强>更新

在move_uploaded_file行之前尝试:

if (! is_writable("/var/www/domain.com/public_html/static_domain/upload/Images/")) {
  die('sorry the web server does not have permission to write to /var/www/domain.com/public_html/static_domain/upload/Images/');
}

答案 1 :(得分:1)

您的代码应如下所示。

$u                  = $_GET['u'];
$fileTmpLoc         = $_FILES["upload"]["tmp_name"];
$fileType           = $_FILES["upload"]["type"];
$fileSize           = $_FILES["upload"]["size"];
$fileErrorMsg       = $_FILES["upload"]["error"];
$kaboom             = explode(".", $fileName);
$fileExt            = end($kaboom);
$fileName           = uniqid() . ".$fileExt"; // $_FILES["upload"]["name"];
$strDestinationPath = "/var/www/domain.com/public_html/static_domain/upload/Images";
if (!$fileTmpLoc)
{
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
}
else if ($fileSize > 5242880)
{
    echo "ERROR: Your file was larger than 5 Megabytes in size.";
    unlink($fileTmpLoc);
    exit();
}
else if (!preg_match("/.(gif|jpg|png)$/i", $fileName))
{
    echo "ERROR: Your image was not .gif, .jpg, or .png.";
    exit();
}
else if ($fileErrorMsg == 1)
{
    echo "ERROR: An error occured while processing the file. Try again.";
    exit();
}
elseif (is_writable($strDestinationPath))
{
    $moveResult = move_uploaded_file($fileTmpLoc, "$strDestinationPath/$fileName");
    if ($moveResult != true)
    {
        $error = error_get_last();
        echo "ERROR: $error[message]";
        exit();
    }
}
else
{
    exit("Directory does not exist or it's not writable");
}
相关问题