<?php
$filename = $_GET['aoutf'];

if( $filename == "" )
{
  echo "<html><title>Force Download Script</title><body>ERROR: download file NOT SPECIFIED. USE downloadFile.php?aoutf=filepath</body></html>";
  exit;
} elseif ( ! file_exists( $filename ) )
{
  echo "<html><title>Force Download Script</title><body>ERROR: File not found. USE downloadFile.php?aoutf=filepath</body></html>";
  exit;
};

$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch( $file_extension )
{
  case "gz"  :  $ctype = "application/x-gzip";            break;
  case "tgz" :  $ctype = "application/x-gzip";            break;
  case "pdf" :  $ctype = "application/pdf";               break;
  case "exe" :  $ctype = "application/octet-stream";      break;
  case "zip" :  $ctype = "application/zip";               break;
  case "doc" :  $ctype = "application/msword";            break;
  case "xls" :  $ctype = "application/vnd.ms-excel";      break;
  case "ppt" :  $ctype = "application/vnd.ms-powerpoint"; break;
  case "gif" :  $ctype = "image/gif";                     break;
  case "png" :  $ctype = "image/png";                     break;
  case "jpeg":
  case "jpg" :  $ctype = "image/jpg";                     break;
//->case "jpg": $ctype = "image/jpeg"; break;
  case "txt" :  $ctype = "text/plain";                    break;
  case "htm" :
  case "html":  $ctype = "text/html";                     break;
//->default: $ctype = "application/octet-stream"; break;
  default: $ctype="application/force-download";
}

header("Pragma: public"); // required
//->header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
//->header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
header("Content-Type: $ctype");
//->header("Content-Type: application/force-download");
// change, added quotes to allow spaces in filenames, by Rajkumar Singh
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
//->header("Content-Transfer-Encoding: $ctype\n"); // Surtout ne pas enlever le \n
header("Content-Length: ".filesize($filename));
readfile("$filename");

?>
