1

I keep getting the invalid file error. Can anyone see what is wrong with this script please. I got it from w3 schools and the folder "pics/2012/Blackhall Primary/" does exist

<?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";
      }
    ?>
5
  • 1
    1. Are you sure you're not exceeding the filesize? 2. Do you have write permissions for the upload folder? Commented Jun 29, 2012 at 15:12
  • var_dump($_FILES) - what are you actually getting? Commented Jun 29, 2012 at 15:13
  • 2
    W3Schools is erroneous in various places...w3fools.com 9Since you mentioned that you got the above script from there... Commented Jun 29, 2012 at 15:13
  • You should also read this: stackoverflow.com/questions/11061355/… Commented Jun 29, 2012 at 15:47
  • start with print_r($_FILES); at the top of your script to see what the data is. Commented Jun 29, 2012 at 15:49

2 Answers 2

1

include more error checking like this:

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

?>
Sign up to request clarification or add additional context in comments.

Comments

0

check the extension of your pics, maybe are in uppercase

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.