PHP 上传代码问题


PHP upload issue with code

我一直收到无效文件错误。任何人都可以看到这个脚本有什么问题。我从w3学校得到它,文件夹"pics/2012/Blackhall Primary/"确实存在

<?php
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/png")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg"))
    && ($_FILES["file"]["size"] < 20000))
      {
      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("pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "pics/2012/Blackhall Primary/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "pics/2012/Blackhall Primary/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>

包含更多错误检查,如下所示:

<?php
echo process_files();
function process_files()
{
    $allowed_file_types=array('image/gif','image/jpeg','image/png','image/jpg','image/pjpeg');
    if(0==sizeof($_FILES)) return 'no files uploaded';
    if(!stristr($_FILES['file']['type'], 'image')) return 'file is not an image';
    if(!in_array($_FILES['file']['type'], $allowed_file_types)) return 'image type '.$_FILES['file']['type'].' is not allowed';
    if($_FILES['file']['size'] < 20000) return 'file size too large. Max:20000, File:'.$_FILES['file']['size'];
    // rest of your code here!
}
?>

检查图片的扩展名,也许是大写的