473,396 Members | 1,749 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Validate whether image, after file upload

hsriat
1,654 Expert 1GB
[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
15 1949
ronverdonk
4,258 Expert 4TB
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
1,654 Expert 1GB
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
4,258 Expert 4TB
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
1,654 Expert 1GB
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
4,258 Expert 4TB
Thanks, I'll look into it.

Ronald
Mar 6 '08 #6
hsriat
1,654 Expert 1GB
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
4,258 Expert 4TB
Thanks for that information. I can certainly use that.

Ronald
Mar 7 '08 #8
hsriat
1,654 Expert 1GB
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
4,258 Expert 4TB
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
1,654 Expert 1GB
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
1,654 Expert 1GB
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
4,258 Expert 4TB
But my avatar is a jpg. Are you telling me they changed it to gif when saving?

Ronald
Mar 7 '08 #13
hsriat
1,654 Expert 1GB
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
4,258 Expert 4TB
You are right. I'll change mine to gif and upload it.

Ronald
Mar 7 '08 #15
hsriat
1,654 Expert 1GB
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

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

Similar topics

15
by: lawrence | last post by:
I've been using the following function (yes, it is inelegant, what can I say, I wrote it a long time ago) to upload images. Haven't had a problem with it for at least a year, and I don't recall...
4
by: DH | last post by:
I have a "file upload form" that works OK, but I have been unsuccessful in my attempt to also resize the uploaded .JPG (if it is too wide), over-writing the original .JPG, and then create and save...
8
by: Dean Speir | last post by:
Hi... I've been referred to this Newsgroup by the W3C Markup Validator FAQ. I've been happily using this Validator <http://validator.w3.org> for the past 18 months with great success, but...
7
by: fingermark | last post by:
I'm writing an upload script and would like to know what is the safest way to validate a file type that is being uploaded to a server? I am accepting just bmp, jpg, png, and gif. Here are is...
2
by: prakharv | last post by:
Hi All, Below is the code which I am using to upload a jpeg file to the server. But the problem I am facing is that it is not copying the entire contents of the image file to the webserver and it...
0
by: aris1234 | last post by:
hello.. How to upload image file in page update ..?? i have logic like this : if user upload new image then old image must delete and update DB used new name if user not upload new image then...
7
by: xx75vulcan | last post by:
Hi, I've got a PHP Upload Form that works great, unless that is, the image your uploading has been modified through a photo editing software. Example: if I upload the image straight from a...
7
by: mishrarajesh44 | last post by:
hii all Truly telling i hav got this code from net & i am finding error while running the code below.. code:- <?php $idir = "photo/"; // Path To Images Directory $tdir =...
1
by: chennaibala | last post by:
can any one send me mutiple image upload program and save the file name with extension in mysql table.we must cheak uploaded file type like bmp or any image file while uploading. i develop...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.