By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,011 Members | 1,068 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,011 IT Pros & Developers. It's quick & easy.

Validate whether image, after file upload

hsriat
Expert 100+
P: 1,654
[PHP]if (!($_FILES['image']['type'] == "image/gif" || $_FILES['image']['type'] == "image/jpg" || $_FILES['image']['type'] == "image/jpeg" || $_FILES['image']['type'] == "image/png"))
die ('Invalid File Type');[/PHP]
This displays error when a file other than image file is uploaded.
But if someone manually changes the mime type, say from .xcf to .png, then this doesn't help.

How can I restrict that?
Mar 5 '08 #1
Share this Question
Share on Google+
15 Replies


ronverdonk
Expert 2.5K+
P: 4,258
What I always have used, but I don't know if it is foolproof, is doing a [php]list($width, $height) = getimagesize($filein);[/php]most non-images, like code, return no width and no height. But, be aware, I have not tested this with more than a couple of file types, so I am not at all sure if there are any other non-image file types, that do return width and height.

Ronald
Mar 5 '08 #2

hsriat
Expert 100+
P: 1,654
Means there is no actual way to find out the file type if messed with the extension.

I have added this to my code, but it will return height and width for other image types also. like tif, jpc etc.

Thanks :)

Harpreet


PS: What's wrong with your Avatar? Can you see it on your browser?
Mar 6 '08 #3

ronverdonk
Expert 2.5K+
P: 4,258
Yes, I know it is not foolproof, but until now it is the only check I have.

My avatar is fine. Have been using it for almost 2 years now and no complaints.

Ronald
Mar 6 '08 #4

hsriat
Expert 100+
P: 1,654
Yes, I know it is not foolproof, but until now it is the only check I have.

My avatar is fine. Have been using it for almost 2 years now and no complaints.

Ronald
Ok, may be there's problem with my browser. Neither your avatar is visible in the contributors list nor near your name. Is written ronverdonk's avatar. Its been like that for the last two days. So I thought of telling you.


Harpreet
Mar 6 '08 #5

ronverdonk
Expert 2.5K+
P: 4,258
Thanks, I'll look into it.

Ronald
Mar 6 '08 #6

hsriat
Expert 100+
P: 1,654
Hi,

While I was searching something on php.net, I found this, exif_imagetype.

This is quite useful for such situations.

Harpreet
Mar 7 '08 #7

ronverdonk
Expert 2.5K+
P: 4,258
Thanks for that information. I can certainly use that.

Ronald
Mar 7 '08 #8

hsriat
Expert 100+
P: 1,654
Thanks for that information. I can certainly use that.

Ronald
Even I want to use it but my WAMP (PHP 5.2.5) says Call of undefined function exif_imagetype()
What can I do with this now? Any idea?
Mar 7 '08 #9

ronverdonk
Expert 2.5K+
P: 4,258
Well I have the same problem. But searching a bit further, I see that the getimagesize() function gives that result also.

In index 2 of the result is the imagetype, identical to the number in exif..().

Ronald
Mar 7 '08 #10

hsriat
Expert 100+
P: 1,654
Well I have the same problem. But searching a bit further, I see that the getimagesize() function gives that result also.

In index 2 of the result is the imagetype, identical to the number in exif..().

Ronald
You are right, 1 is for gif, 2 is for jpg, 3 for png and 7 for tif.
I didn't have any other formats, so could not check.

Apart from that, the mime index actually returns the file type, even if extension is fake. Good information for me.

Thanks

Harpreet
Mar 7 '08 #11

hsriat
Expert 100+
P: 1,654
My avatar is fine. Have been using it for almost 2 years now and no complaints.

Ronald
Now I know why I can't see your Avatar sometimes.
While I was working on different image formats, I tried displaying a jpg on the browser after changing its extension to gif.
It gave error sometimes (1 out of 10 tries).

Your Avatar is saved in theScripts as gif, while its a jpg image.

Harpreet
Mar 7 '08 #12

ronverdonk
Expert 2.5K+
P: 4,258
But my avatar is a jpg. Are you telling me they changed it to gif when saving?

Ronald
Mar 7 '08 #13

hsriat
Expert 100+
P: 1,654
But my avatar is a jpg. Are you telling me they changed it to gif when saving?

Ronald
No they didn't convert it, but just changed the extension.
Right click on your avatar and save it. It will tell you that its actually jpg but name says its gif
Mar 7 '08 #14

ronverdonk
Expert 2.5K+
P: 4,258
You are right. I'll change mine to gif and upload it.

Ronald
Mar 7 '08 #15

hsriat
Expert 100+
P: 1,654
You are right. I'll change mine to gif and upload it.

Ronald
Yeah, you may.
Even mine was PNG, and I changed it to GIF. See now its only visible near my name, but not in the contributors list :D. This is because, they don't link to the latest Avatar in the contributors list. That will be changed when the contributor's list will get updated next time. You can see it still refers to the old serial number, while one in the forum refers to the new serial number.

Have a good luck!

Harpreet
Mar 7 '08 #16

Post your reply

Sign in to post your reply or Sign up for a free account.