Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.