PHP File Upload

I got this code from http://w3schools.com/. When I try to upload and .gif, .jpg, or .jpeg file, It returns “Invalid file”. I know the file is the right size, but I can’t find the error. Can someone help me debug this?

Thanks,
BM

Well what the hell is your code?

Seriously…

My bad. :blush:

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_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("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";
  }
?> 

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))

This line is failing. Can you do this and post the output:


var_dump($_FILES['file']);
die();

array(5) { ["name"]=>  string(11) "Telex2.jpeg" ["type"]=>  string(10) "image/jpeg" ["tmp_name"]=>  string(26) "C:\\WINDOWS\\Temp\\php2F6.tmp" ["error"]=>  int(0) ["size"]=>  int(107053) } 

Then there is the problem: Your code wants file size to be less than 20k, but you are uploading one sized 107k.