Reputation: 16194
I Have to download the listed file on click of the link, for whic I have used the below scripot, but I when the file gets dowloaded, it cannot idenetify the extension of downloaded file. So, How to get MIME Type of a file to be downloaded? _Please Help...
$filename = $_GET['val'];
// Fetch the file info.
$filePath = $_SERVER['DOCUMENT_ROOT'] . "dfms/images/uploads/".$filename;
if(file_exists($filePath)) {
echo $fileName = basename($filePath);
$fileSize = filesize($filePath);
// Output headers.
header("Cache-Control: private");
header("Content-Type: application/octet");
header("Content-Length: ".$fileSize);
header("Content-Disposition: attachment; filename=".$fileName);
// Output file.
readfile ($filePath);
exit();
}
else {
die('The provided file path is not valid.');
}
Upvotes: 3
Views: 15140
Reputation: 8334
Using the finfo_file function from the FileInfo extension (enabled by default in PHP 5.3). http://www.php.net/manual/en/function.finfo-file.php
From the example in the documentation
$finfo = finfo_open(FILEINFO_MIME_TYPE);
echo finfo_file($finfo, $filename);
finfo_close($finfo);
In PHP versions prior to 5.3 the pecl extension can be installed http://pecl.php.net/package/Fileinfo
However in this case it requires the magic_open (libmagic) library http://sourceforge.net/projects/libmagic
The alternative is to use the deprecated function mime_content_type($filename)
http://au.php.net/manual/en/function.mime-content-type.php
Which relies on the mime.magic file
Upvotes: 6