文件上传脚本引发错误:"Invalid File"



我是php的新学习者。我试图上传一个简单的东西,但它不工作…帮我弄清楚我在w3学校提到过…我有一个密码…我创建了一个名为upload的文件夹。为什么它不工作…

<html>
<body>
 <form action="upload_file.php" method="post"
 enctype="multipart/form-data">
 <label for="file">Filename:</label>
 <input type="file" name="file" id="file"><br>
  <input type="submit" name="submit" value="Submit">
  </form>
   </body>
 </html>

upload_file.php

        <?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/pjpeg")
        || ($_FILES["file"]["type"] == "image/x-png")
        || ($_FILES["file"]["type"] == "image/png"))
        && ($_FILES["file"]["size"] < 20000)
        && 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";
        }
     ?>

增大文件大小并添加允许的大写扩展名。我相信有更好的方法,但它有效。我将20000增加到20000000

<?php
     $allowedExts = array("gif", "GIF", "jpeg", "JPEG", "jpg", "JPG", "png", "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/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000000) // increased allowed size may be your problem
    && 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";
    }
?>

最新更新