Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is this a good filter for uploaded files?

Tags:

security

php

I have a site on which users can upload files to a subdirectory. I'm filtering the uploads check for potentially malicious code. I'm new to the security side of things, so does this look like a best-practice for securing uploads to the server? If not or if I'm missing anything, could you point me in the right direction?

//arrays with acceptable file extensions/types -- default validations set to false
$acceptable_ext = array('jpg', 'JPG', 'jpeg', 'JPEG', 'gif', 'GIF', 'png', 'PNG');
$acceptable_type = array('image/jpeg', 'image/gif', 'image/png');
$validated_ext = 0;
$validated_type = 0;

//validate file extension and type
if($_FILES && $_FILES['file']['name']) {

    $file_info = pathinfo($_FILES['file']['name']);

    //validate extension
    for ($x=0; $x < count($acceptable_ext); $x++) { 
        if($file_info['extension'] == $acceptable_ext[$x]) {
            $validated_ext = 1;
        }
    }
    //validate type
    for ($x=0; $x < count($acceptable_type); $x++) { 
        if($file_info['type'] == $acceptable_type[$x]) {
            $validated_type = 1;
        }
    }
}

if($validated_ext && $validated_type) {
   //upload file to the server blah blah
}
like image 614
Hat Avatar asked Nov 13 '22 15:11

Hat


1 Answers

You can check out some security configurations here.

Checking MIME types, ini config, .htaccess etc will give you extra security or extra validation as per the link.

like image 141
Vishnu R Avatar answered Nov 15 '22 05:11

Vishnu R