Restrict File Types for Upload

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
devarishi
Forum Contributor
Posts: 101
Joined: Fri Feb 05, 2010 7:15 pm

Restrict File Types for Upload

Post by devarishi »

How to restrcit selections of files for upload - client / browser side?

The attribute accept="image/jpeg" doesn't seem to have any effect in file tag of HTML.

Note: I am using HTML and PHP only and am not familiar with Ajax or any other such tools. I want users to be able to browse and select only image (.jpg) files.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Restrict File Types for Upload

Post by yacahuma »

http://www.webcheatsheet.com/PHP/file_upload.php


you restrict using the type property

$_FILES["uploaded_file"]["type"]
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Restrict File Types for Upload

Post by requinix »

I don't know of any browser that honors the accept property.

Also, using $_FILES[...]["type"] is very risky. It's supplied by the browser which means a malicious user could fake it.
Figure out the file type yourself. For images you can use getimagesize; for others you can make something yourself or use the fileinfo extension.
Post Reply