PHP and image formats

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rtlm
Forum Newbie
Posts: 5
Joined: Thu Nov 04, 2004 6:09 am

PHP and image formats

Post by rtlm »

Hi all,
I have a php script which users can upload images to. Are there any functions I can use to ensure that the users only upload images with certain extentions?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

When you upload a file via HTTP, there is an element in the $_FILES array that stores the MIME type of the upload.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

see also [php_man]getimagesize[/php_man]().
Getran
Forum Commoner
Posts: 59
Joined: Wed Aug 11, 2004 7:58 am
Location: UK
Contact:

Post by Getran »

You could also do a check before uploading to check the type of file, maybe something like:

Code: Select all

if ($filevar != "image/gif") {
  echo "File Type Illegal";
} else {
  // Upload
}
I used the image/gif thing once before but i don't remember how i got it working, i'm sorry about that but hopefully this will give you an idea...
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Yeah, the 'type' thing outputs the mime type like:
image/jpg
image/gif etc...

I have however experienced problems with this for instance when a user uploads some files the type == NULL for some reason and I am forced to trust the file extension, but some one could just change the file extension and upload something.exe.jpg..... Why does the type come out to NULL? Is there any other ways to check the encoding of the file?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

Post by phpScott »

phpScott wrote:see also [php_man]getimagesize[/php_man]().
see also
[php_man]mime_content_type[/php_man]()
Post Reply