I'm a PHP beginner and currently learning the "Validating the File Upload" part.
I made a test.php page containing following code:
var_dump(@$_FILES['file']['type']);
First, I uploaded an image "img.gif" and it returned:
string 'image/gif' (length=9)
Then, I changed the image's extension to ".jpg" and it returned:
string 'image/jpeg' (length=10)
So I realized $_FILES["file"]["type"] only return the uploaded file extension but didn't actually check what file is it.
In this page, http://www.w3schools.com/php/php_file_upload.asp, there is a code:
$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/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
I'm wondering why above codes check file extension twice? I deleted some from above codes and this is my new code:
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if (($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts))
Is my code correct? Or do you have any better ways to validate the upload file is a image?
Thanks!