Jump to content

[SOLVED] File type validation


tjodolv

Recommended Posts

I am writing a script for uploading files. I want the script to only accept certain filetypes, in order to avoid any bad content. I figured I could do it by checking the MIME-type, or perhaps the file-ending. However, there are quite a number of filetypes I would like to allow, and so I am wondering if I could get some pointers to a more elegant solution than simply "brute-forcing" my way through all the different filetypes.?

Link to comment
https://forums.phpfreaks.com/topic/63444-solved-file-type-validation/
Share on other sites

The brute force should be:

 

$ext = strtolower(substr(strrchr($filename, '.'), 1));
if($ext == 'jpg' or $ext == 'doc' etc etc){
    ...some code
}

 

or

 

$ext = strtolower(substr(strrchr($filename, '.'), 1));
$allowedExt = array('jpg', 'doc' etc etc);
if(in_array($ext, $allowedExt){
   ...some code
}

 

U can fill the array with the allowed extension types and u'll have a much cleaner code, i guess :). Dont know of any other method.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.