PHP move_uploaded_file()错误

时间:2013-12-19 04:27:04

标签: php file-upload

我知道这有点生硬,但以下是我现在约3小时的主要关注点,我需要帮助。 我检查了文件权限(777)。而且,也许是最讨厌的部分,它昨天正在工作! “if”语句没问题,因为我通过回显语句来检查它是否move_uploaded_file()。我在某个时候遇到错误,但我认为已经过了,错误是:

[Thu Dec 19 03:53:06 2013] [error] [client 46.208.50.146] File does not exist: /some_directory/[object File], referer: http://my_domain/

代码:

$rand_string = "";
$rand_file_string = "";
$characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
for ($i = 0; $i < 30; $i++) {
    $rand_string .= $characters[rand(0, strlen($characters) - 1)];
}
for ($i = 0; $i < 30; $i++) {
    $rand_file_string .= $characters[rand(0, strlen($characters) - 1)];
}
$fileNameMain = $rand_file_string . "-" . str_replace(" ", "", $_FILES["input_mainpic"]["name"]);
$fileTmpLoc = $_FILES["input_mainpic"]["tmp_name"];
$pathAndName =  "../../../../../my_domain.com/htdocs/southwye/upload/" . $rand_file_string . "-" . $fileNameMain;
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["input_mainpic"]["name"]);
$extension = strtolower(end($temp));
if ((($_FILES["input_mainpic"]["type"] == "image/gif")
|| ($_FILES["input_mainpic"]["type"] == "image/jpeg")
|| ($_FILES["input_mainpic"]["type"] == "image/jpg")
|| ($_FILES["input_mainpic"]["type"] == "image/pjpeg")
|| ($_FILES["input_mainpic"]["type"] == "image/x-png")
|| ($_FILES["input_mainpic"]["type"] == "image/png"))
&& ($_FILES["input_mainpic"]["size"] < 2000000)
&& in_array($extension, $allowedExts)){
  if ($_FILES["input_mainpic"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["input_mainpic"]["error"] . "<br>";
    }
  else
    {
    if (file_exists($rand_file_string . $_FILES["input_mainpic"]["name"]))
      {
      echo $rand_file_string . $_FILES["input_mainpic"]["name"] . " already exists. ";
      }
    else
      {
        move_uploaded_file($fileTmpLoc, $pathAndName);
      }
    }
  }

请不要错误的想法......我实际上已经对此进行了调查,但是我已经做错了!运行代码的文件具有777权限。我试图使用不同的目录并给它所需的权限但没有任何反应。

我完全没有得到PHP的反馈(我假设代码中没有错误)

2 个答案:

答案 0 :(得分:2)

我认为您推荐的临时文件位置错误

而不是

$fileTmpLoc = "/temp/" . $_FILES["input_mainpic"]["tmp_name"];

你必须使用

$fileTmpLoc = $_FILES["input_mainpic"]["tmp_name"];

答案 1 :(得分:0)

假设您的上传文件路径文件夹无法访问您的文件夹路径,我已按以下方式重写,

通过手动 "../../../../../my_domain.com/htdocs/southwye/upload/";添加下面提到的代码,可以直接恢复站点路径。这可能是文件上传错误的原因

    $filePath = realpath(dirname(__FILE__));
    $rootPath = realpath($_SERVER['DOCUMENT_ROOT']);
    $htmlPath = str_replace($root, '', $filePath);
    $fileNameMain = $rand_file_string . "-" . str_replace(" ", "", $_FILES["input_mainpic"]["name"]);
    $pathAndName =  $htmlPath."/upload/" . $rand_file_string . "-" . $fileNameMain;
    $fileTmpLoc = $_FILES["input_mainpic"]["tmp_name"];