Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the content-type of a file in PHP?

Tags:

I'm using PHP to send an email with an attachment. The attachment could be any of several different file types (pdf, txt, doc, swf, etc).

First, the script gets the file using "file_get_contents".

Later, the script echoes in the header:

Content-Type: <?php echo $the_content_type; ?>; name="<?php echo $the_file_name; ?>" 

How to I set the correct value for $the_content_type?

like image 488
edt Avatar asked Aug 05 '09 11:08

edt


People also ask

How do I find the content type of a file?

There are three sets of tests, performed in this order: filesystem tests, magic number tests, and language tests. The first test that succeeds causes the file type to be printed. If you're on a Windows machine it is common to use the file extension like you showed in your code.

How do I read the contents of a file in PHP?

The file_get_contents() reads a file into a string. This function is the preferred way to read the contents of a file into a string. It will use memory mapping techniques, if this is supported by the server, to enhance performance.

How do I echo content in PHP?

readfile("/path/to/file"); This will read the file and send it to the browser in one command. This is essentially the same as: echo file_get_contents("/path/to/file");


2 Answers

I am using this function, which includes several fallbacks to compensate for older versions of PHP or simply bad results:

function getFileMimeType($file) {     if (function_exists('finfo_file')) {         $finfo = finfo_open(FILEINFO_MIME_TYPE);         $type = finfo_file($finfo, $file);         finfo_close($finfo);     } else {         require_once 'upgradephp/ext/mime.php';         $type = mime_content_type($file);     }      if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) {         $secondOpinion = exec('file -b --mime-type ' . escapeshellarg($file), $foo, $returnCode);         if ($returnCode === 0 && $secondOpinion) {             $type = $secondOpinion;         }     }      if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) {         require_once 'upgradephp/ext/mime.php';         $exifImageType = exif_imagetype($file);         if ($exifImageType !== false) {             $type = image_type_to_mime_type($exifImageType);         }     }      return $type; } 

It tries to use the newer PHP finfo functions. If those aren't available, it uses the mime_content_type alternative and includes the drop-in replacement from the Upgrade.php library to make sure this exists. If those didn't return anything useful, it'll try the OS' file command. AFAIK that's only available on *NIX systems, you may want to change that or get rid of it if you plan to use this on Windows. If nothing worked, it tries exif_imagetype as fallback for images only.

I have come to notice that different servers vary widely in their support for the mime type functions, and that the Upgrade.php mime_content_type replacement is far from perfect. The limited exif_imagetype functions, both the original and the Upgrade.php replacement, are working pretty reliably though. If you're only concerned about images, you may only want to use this last one.

like image 184
deceze Avatar answered Sep 20 '22 14:09

deceze


It very easy to have it in php.

Simply call the following php function mime_content_type

<?php     $filelink= 'uploads/some_file.pdf';     $the_content_type = "";      // check if the file exist before     if(is_file($file_link)) {         $the_content_type = mime_content_type($file_link);     }     // You can now use it here.  ?> 

PHP documentation of the function mime_content_type() Hope it helps someone

like image 21
BoCyrill Avatar answered Sep 20 '22 14:09

BoCyrill