I am starting PHP and am currently studying the part "Checking File Upload".
I made a test.php page containing the following code:
var_dump(@$_FILES['file']['type']);
First I uploaded the image "img.gif" and it returned:
string 'image/gif' (length=9)
Then I changed the image extension to ".jpg" and it returned:
string 'image/jpeg' (length=10)
So, I realized that $ _FILES ["file"] ["type"] returns only the extension of the downloaded file, but did not really check what the file was.
On this page http://www.w3schools.com/php/php_file_upload.asp there is 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 am wondering why the above codes check the file extension twice? I deleted some of the previous 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 check the download file - is this an image?
Thanks!