Validating Image files using php

Hi,
I am posting the file from client side HTML and I wrote a PHP script at server side to check whether the uploaded file is image or not as if($_FILES[“file”][“type”]==“image/jpg”…).
But this code does not check the actual content of the file uploaded,i.e if someone renames other file types as .jpg or .png etc then my script takes it as image file.

Anybody please help me.

Thanks.

According to http://corpocrat.com/2007/11/28/implementing-secure-file-upload-in-php/ you can use a method similar to this:

[php]//check if its image file

if (!getimagesize($_FILES[‘imagefile’][‘tmp_name’]))
{ echo “Invalid Image File…”;
exit();
}
[/php]

May I know what does ‘filename’ and’ tmp_name’ stands for?

May I know what does ‘imagefile and’ tmp_name’ stands for?

‘imagefile’ is the name of the file input, and ‘tmp_name’ is the temporary name given to it when it was uploaded - you only need to change ‘imagefile’.

Still the script takes the other type of the file which I renamed as .jpg(Not a image file)
my HTML:

my Script:
if(!getimagesize($_FILES[‘photo’][‘tmp_name’]))

This works for me. What type of file have you renamed? Can you post the full name of the renamed file?

The file is of type .doc

I don’t know how it would work for the makers of the tutorial and me but not for you. Do you get any errors? If not, try putting this at the start of your code - just to make sure there aren’t any errors not being shown:

[php]error_reporting(E_ALL);[/php]

I’m not getting any error message in the browser .I have tried error_reporting.

Now it works fine thanks a lot

Hey I need one more help how can I display the image stored in mysql into webpage?

How is the image stored into MySQL? Do you store the image path?

No I have stored entire image data in the mysql.Is it a good way of storing?or shall I store image path?

I would store the path to where the file is hosted on your webserver.

Sponsor our Newsletter | Privacy Policy | Terms of Service