1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
// $file est le chemin absolu du fichier : /path/to/file.ogv
function file_download($file)
{
if( headers_sent() ) { die('Headers Sent'); }
if (!file_exists($file)) { die('File not found'); }
if (!is_file($file)) { die('File not found'); }
if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); // IE
// Infos sur le fichier
$len = filesize($file);
$filename = basename($file);
// $parts = pathinfo($fullPath);
// $file_extension = strtolower($parts["extension"]);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
// Définit le Content-Type selon le type de fichier
switch( $file_extension ) {
case "avi" : $ctype="video/x-msvideo"; break;
case "doc" : $ctype="application/msword"; break;
case "exe" : $ctype="application/octet-stream"; break;
case "gif" : $ctype="image/gif"; break;
case "jpg" :
case "jpeg" : $ctype="image/jpg"; break;
case "mp3" : $ctype="audio/mpeg"; break;
case "mp4" : $ctype="video/mp4"; break;
case "mpeg" :
case "mpg" :
case "mpe" : $ctype="video/mpeg"; break;
case "mov" : $ctype="video/quicktime"; break;
case "ogv" : $ctype="video/ogg"; break;
case "pdf" : $ctype="application/pdf"; break;
case "png" : $ctype="image/png"; break;
case "ppt" : $ctype="application/vnd.ms-powerpoint"; break;
case "wav" : $ctype="audio/x-wav"; break;
case "xls" : $ctype="application/vnd.ms-excel"; break;
case "xlsx" : $ctype="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break;
case "zip" : $ctype="application/zip"; break;
// Ces extensions ne doivent pas être téléchargées
case "php":
case "htm":
case "html":
case "sql":
case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;
default: $ctype="application/force-download";
}
// Ecriture du header
header("Cache-Control: must-revalidate, no-cache, no-store"); // HTTP/1.1
header("Cache-Control: private, post-check=0, pre-check=0", false); // IE
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename.";");
header("Content-Length: ".$len);
header("Content-Transfer-Encoding: binary");
header("Content-Type: $ctype");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date dans le passé
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . " GMT"); // Date GMT
header("Pragma: no-cache");
ob_clean();
flush();
// Download file
readfile($file);
die();
} |
Partager