1

我不断收到无效文件错误。谁能看看这个脚本有什么问题。我从 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";
      }
    ?>
4

2 回答 2

1

包括更多这样的错误检查:

<?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!
}

?>
于 2012-06-29T15:59:17.037 回答
0

检查你的图片的扩展名,可能是大写的

于 2012-06-29T17:13:56.393 回答