0

我是 php 的新手。我试图上传一个简单的东西,但它不起作用..帮助弄清楚我在 w3school 中提到过..我有一个代码..我创建了一个名为上传的文件夹..为什么它不起作用..

<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>

上传文件.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";
        }
     ?>
4

1 回答 1

1

增加文件大小并允许添加大写扩展名。确信有更好的方法可以做到这一点,但它确实有效。我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";
    }
?>
于 2013-04-26T15:20:30.140 回答