PHP上传程序无效


php uploader invalid

我试图嵌入文件上传器,但它显示pdf和其他应用程序文件无效错误,以下代码我正在使用,谁能帮助我跟踪为什么它显示无效消息的错误。提前谢谢。

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png" );
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "application/pdf"))
&& ($_FILES["file"]["size"] < 50000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
  {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
  else
{
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
    }
          }
    else
      {
      echo "Invalid file";
  }
?>

您只允许.gif, .jpeg, .jpg, .png在第1行:

$allowedExts = array("gif", "jpeg", "jpg", "png" );

如果你上传PDF,它当然会提示Invalid file

   $allowedExts = array("gif", "jpeg", "jpg", "png" )

不包含"pdf",请添加允许的扩展名"$allowedExts",如

     $allowedExts = array("gif", "jpeg", "jpg", "png","pdf","php" );

请尝试以下操作:

use: $allowedExts = array("gif", "jpeg", "jpg", "png","pdf");

代替:$allowedExts = array("gif", "jpeg", "jpg", "png" );

对于将来,请记住添加您想要允许的扩展。

这个明显的毛病已经指出来了。

除此之外,如果你重构和重新格式化你的代码,像

$allowedExts = Array("gif", "jpeg", "jpg", "png" );
$allowedContentTypes = Array("application/pdf", "image/png", "image/jpeg", "image/jpg", "image/gif");
$fileSizeLimit = 50000;
function getExtension($file)
{
  return end(explode(".", $file["name"]));
}
function hasAllowedContentType($file)
{
  return in_array($file['type'], $allowedContentTypes);
}
function isWithinSizeLimits($file)
{
  return $file['size'] < $fileSizeLimit;
}
function hasAllowedExtension($file)
{
  $extension = getExtension($file['name');
  return in_array($extension, $allowedExts);
}
function alreadyExists($path)
{
  return file_exists($path);
}
$file = $_FILES["file"];
$targetPath = "upload/" . $file["name"];
$tempPath = $file["tmp_name"];
if (!hasAllowedContentType($file)
   || !isWithinSizeLimits($file)
   || !hasAllowedExtension($file))
{
   echo "Invalid file";
}
else if ($file["error"] > 0)
{
   printf("Return Code: %s<br>", $file["error"]);
}
else
{
  printf("Upload: %s<br>", $file["name"]);
  printf("Type: %s<br>", $file["type"]);
  printf("Size: %d kB<br>", ($file["size"] / 1024));
  printf("Temp file: %s<br>", $tempPath);
  if (alreadyExists($targetPath))
  {
    printf("%s already exists.", $file["name"]);
  }
  else
  {
    move_uploaded_file($tempPath, $targetPath);
    printf("Stored in: %s", $targetPath);
  }
}

它已经很容易理解和维护了