finding a file type

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
hame22
Forum Contributor
Posts: 214
Joined: Wed May 11, 2005 5:50 am

finding a file type

Post by hame22 »

Hi I am allowing users to upload files to my site.

However I would like to restrict them to just PDF, word files and powerpoint presentations.

Is there a function like getimagesize to allow this?
timclaason
Forum Commoner
Posts: 77
Joined: Tue Dec 16, 2003 9:06 am
Location: WI

File types

Post by timclaason »

I would use substr().

For instance:

Code: Select all

if(substr($_FILES['theimage']['name'], -4) != ".doc") {
   print("Invalid File Type");
}
or preg_match() or some other string function.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

And if I upload an vbscript that I named funny.gif (or funny.doc)? This really worked in IE some time ago and the script got executed. Don't rely on a simple file suffix.
Take a look at http://de2.php.net/ref.mime-magic
Post Reply