Harini
Harini

Reputation: 3

File uploading script throwing error: "Invalid File"

i am new leaner for php. i was trying to upload a simple thing but it does not works.. help to figure out i have referred in w3school.. i have a code.. and i have create a folder called upload.. why its not works..

<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";
        }
     ?>

Upvotes: 0

Views: 1581

Answers (1)

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

Increase your file size and add uppercase extensions allowed. Am sure there's a better way to do this, but it works. I increased 20000 to 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";
    }
?>

Upvotes: 1

Related Questions