3

What are the supported file types in php file upload?

   Choose a file to upload: <input name="uploadedfile" type="file" />
   <br />
      Username:<input type="text" name="username">
   <br />
      Password:<input type="text" name="password">
   <br />
      FaxNumber:<input type="text" name="faxnumber">

   <input type="submit" value="Upload File" />

Community
  • 1
  • 1
user1036474
  • 47
  • 1
  • 2
  • 7

7 Answers7

4

PHP does not limit anything like this. A file is always just data. PHP gives you that data when it's "posted" (or uploaded) to your server. It doesn't look at it to determine what kind of data it is, it's really just saying "this data was uploaded, here's the path to the temporary file." Anything is acceptable to PHP, as long as your server can handle receiving the entire file. Whether or not you want to limit what types can be uploaded is completely up to you.

animuson
  • 53,861
  • 28
  • 137
  • 147
1

Maybe you need this : http://en.wikipedia.org/wiki/Internet_media_type

There is no limit on file type by default,till you specify a restriction.

The file type of an uploaded file is available by :

$_FILES['yourFileName']['type']
ABS
  • 2,626
  • 3
  • 28
  • 44
1

There is no limit client side, all files/types are supported. PHP, also, has no built in limits and supports all files. If you want to limit what can be uploaded in php it looks something like the following snippet, Which limits the file to a gif image:

if ($_FILES["file"]["type"] == "image/gif"){
    //do stuff here
}
else{
    //the file was wrong type handle error here
}

You can find a list of MIME types, "image/gif" in the above code, at:
http://en.wikipedia.org/wiki/Internet_media_type#List_of_common_media_types

vdbuilder
  • 12,254
  • 2
  • 25
  • 29
  • 2
    Don't use `$_FILES['type']`, it's completely arbitrary, unverified data. – deceze Jan 16 '12 at 05:47
  • 1
    @deceze I'll agree that many other steps are required to ensure server security, such as; using .htaccess files, uploading to directory outside server root, preventing overwriting of existing files, etc. However ,they just seemed to be outside the scope of the question. $_FILES['type'] is not necessarily valid, but if it's set to something other than what is allowed we can stop the upload at this point. – vdbuilder Jan 16 '12 at 06:30
  • Image types are *largely* recognized correctly, but other file types may or may not have their correct MIME type set by the client. Therefore, you may get false negatives. Further, this value is trivial to spoof, so using this as your only file type check is insecure (security depending on what you do with these files). Therefore, if you're *really* interested in the MIME type, you'll have to do your own check anyway. – deceze Jan 16 '12 at 06:55
0

You can upload any file, php doesn't limit it. You self need to do the restrict.

For client file format limit, refer to this Limit file format when using ?

<input type="file" accept="image/*" /> <!-- all image types --> 
<input type="file" accept="audio/*" /> <!-- all audio types --> 

For server, you can filter the uploaded file by this,

if(in_array(mime_type($file_path),$allowed_mime_types)){
    // save the file
}

$allowed_mime_types = array(
        'image/jpeg',
        'image/jpg',
        'image/png',
        'image/gif',
        'video/mp4'
);


/*
For PHP>=5.3.0, you can use php's `finfo_file`([finfo_file](https://www.php.net/manual/en/function.finfo-file.php)) function to get the file infomation about the file.

For PHP<5.3.0, you can use your's system's `file` command to get the file information.
*/
function mime_type($file_path)
{
    if (function_exists('finfo_open')) {            
        $finfo = new finfo(FILEINFO_MIME_TYPE, null);
        $mime_type = $finfo->file($file_path);
    }
    if (!$mime_type && function_exists('passthru') && function_exists('escapeshellarg')) {
        ob_start();
        passthru(sprintf('file -b --mime %s 2>/dev/null', escapeshellarg($file_path)), $return);
        if ($return > 0) {
            ob_end_clean();
            $mime_type = null;
        }
        $type = trim(ob_get_clean());
        if (!preg_match('#^([a-z0-9\-]+/[a-z0-9\-\.]+)#i', $type, $match)) {
            $mime_type = null;
        }
        $mime_type = $match[1];
    }
    return $mime_type;
}
LF00
  • 27,015
  • 29
  • 156
  • 295
0
by default: nearly anything you want (pdf,txt,exe,jpg) etc.

Its your job to filter out anything you do wish to use. you can filter against anything inside $_FILES array(type,size) etc

Philip
  • 4,592
  • 2
  • 20
  • 28
0

PHP will support all file types for file upload, But you have to validate for some file extensions which can hack your site and also you have to consider file size to avoid long running script.

Robin Michael Poothurai
  • 5,444
  • 7
  • 23
  • 36
0

PHP does not handle uploads. Uploads are supported by the http specification, and should be handled by your webservice (Apache, IIS, etc).

Regardless, if you wish to just save the uploaded file, then all file types should work fine. If you want to process the uploaded file as input, things get much more hairy.

MrGlass
  • 9,094
  • 17
  • 64
  • 89