In php I can check if a uploaded file has proper type by extension, so code should look like this:
if ((($_FILES["photo1"]["type"] == "image/gif")
|| ($_FILES["photo1"]["type"] == "image/jpeg")
|| ($_FILES["photo1"]["type"] == "image/png"))
&& ($_FILES["photo1"]["size"] < 500000)) //also limiting size
Then in next step in my code I prepare a file for further processing. But what if someone changes a text_file.doc or javascript_file.js to samplefile.jpg before upload?
move_uploaded_file(($_FILES['photo1']['tmp_name']), "photos/1.jpg");
$source1 = imagecreatefromjpeg("../photos/source1.jpg");
Then user will see errors from imagecreatefromjpeg step:
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: gd-jpeg: JPEG
library reports unrecoverable error: in...
How to skip a processing part if a file is not a graphic file and not display errors?
I would use
getimagesizeand check for possible errors, something like this:This GD function is not perfect, but it can cope with several image file formats.
There are several ways to omit the warnings in PHP. If an error like this can happen, it usually will happen. Either expect it in your code (usually preferrable, see my example with try…catch) or configurate your enviroment to your needs (p.e. omit warnings).