Archivo no válido en la carga de archivos PHP

I've built a website with a HTML form/ PHP upload for image files, it works well when its running on XAMPP on my local computer but when i've uploaded it to 000webhost most of the time it says invalid file and only sometimes will the images successfully upload. I've tried turning up the max execution time in the php configuration but that doesn't seem to have fixed it. The files i've tried to upload are smaller than the max file size in the php config and have worked on my test machine perfectly.

I find it odd that it works sometimes and doesn't other times and don't really know what to try.

EDIT:

Here is the form Filename:

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png")))
   {
       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 />";


          move_uploaded_file($_FILES["file"]["tmp_name"],
          "churchimages/pauls.jpeg");
          echo "Stored in: " . "churchimages/pauls.jpeg";

          }
  }

 else

 {
 echo "Invalid file";//This is the section I am seeing
 }

preguntado el 27 de agosto de 11 a las 16:08

You will need to show some source code and the exact error message that you get. -

but when i've uploaded it to 000webhost most of the time it says invalid file and only sometimes will the images successfully upload. that inconsistent behaviour sounds like hosting issues rather than your code... -

@toopay and Pekka I've added my code now -

5 Respuestas

Your file type detection is relying on $_FILES["file"]["type"], which is sent by the browser and highly unreliable.

A much better way to detect whether an uploaded file is an image is getimagesize().

$info = getimagesize($_FILES["file"]["tmp_name"]);

$allowed_types = array(IMG_GIF, IMG_JPEG, IMG_PNG);

if (in_array($info[2], $allowed_types))
 { .... do stuff ... }

Respondido 27 ago 11, 20:08

Thanks for the help, that worked brilliant. I got the code I was using off W3 schools here w3schools.com/php/php_file_upload.asp I wonder why they don't recommend this method instead. - Joel

Wait no, this hasn't fixed it, it works sometimes but other times it says Warning: getimagesize() [function.getimagesize]: Filename cannot be empty, I'm guessing this means that sometimes the file isnt being transferred between pages correctly - Joel

in that case, the upload fails. Use a combination of my and @Marc's answer to make sure that the image gets uploaded, and that it's an image. - Pekka

I think that there's something blocking too many uploads in a certain time because I tried to do it following yours and Marc's advice but when my original said invalid file I got Filename cannot be empty from the getimagesize function - Joel

Could a memory limit cause this error? On my local testing server it is 128M but on 000Webhost it's 64MB, will this be a problem for image files? - Joel

You should check if the upload actually succeeded FIRST, before doing any of the other operations:

if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
    ... handle upload...
    if (!move_uploaded_file(...)) { // <--important. ALWAYS check if the move worked.
       die("File move failed. Data is lost");
    }
} else {
   die("Upload failed with code " . $_FILES['file']['error']);
}

IN the case where an upload did not occur, then the ['type'] field would not be set, and just saying "invalid file" would be useless - there is no file at all.

As Pekka's pointed out, you should use other methods of determining file type. The ['type'] data in $_FILES is user-provided, and is trivial to forge.

Respondido 27 ago 11, 21:08

Thanks for the help, I will do this - Joel

Make sure that you're defining an absolute path for the uploaded files rather than a relative one.

Respondido 27 ago 11, 20:08

I have had this issue today.. very sad. took about an hour to repair.

Idea: checking for $_FILES["file"]["type"] == "image/gif" at the top of the script is what failed for me. It seems the browser was populating a "BLANK DATA" instead of "image/gif" so my initial check failed each time with "invalid file type"

Pregunta: It seems the browsers are not properly sending across the file type from the tmp directory?

My FIX: remove the file type check.... as stated above it is very unreliable...

contestado el 17 de mayo de 13 a las 20:05

Check the PHP file permissions. in ooowebhost you can find it in chmod. change to '777'. otherwise, the file doesn't have permissions to execute/write

contestado el 22 de mayo de 14 a las 14:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.