在PHP中从localhost站点下载文件

时间:2012-09-18 06:04:53

标签: php

当我下载一个带有mp3,flv等扩展名的文件时,它现在会下载它开始缓冲当我从文件中删除扩展时它会下载......

你可以告诉我们背后的问题......

要下载的文件是=> theangelfoundation-12-5-2012-09-17-27-somebody.mp3

提前致谢..

1 个答案:

答案 0 :(得分:0)

 $file_types=array();

             $file_types['mp3']   ='audio/mpeg';
             $file_types['mpeg']  ='video/mpeg';
             $file_types['mpg']   ='video/mpeg';
             $file_types['pdf']   ='application/pdf';
             $file_types['pps']   ='application/vnd.ms-powerpoint';
             $file_types['ppt']   ='application/vnd.ms-powerpoint';
             $file_types['ps']    ='application/postscript';
             $file = 'you.mp3';  
             download($file,$file_types);    




function download($file_name,$file_types){

          $file = $file_name;
          $ext = end(explode('.',$file_name));
          if($ext && array_key_exists($ext,$file_types)){
          if (file_exists($file)) {
             header('Content-Description: File Transfer');
             header('Content-Type: application/octet-stream');
             header('Content-Disposition: attachment; filename='.basename($file));
             header('Content-Transfer-Encoding: binary');
             header('Expires: 0');
             header('Cache-Control: must-revalidate');
             header('Pragma: public');
             header('Content-Length: ' . filesize($file));
             ob_clean();
             flush();
             readfile($file);
             exit;
             }
          }
         else {
            die("this is not a downloadable file");
             }




        }


?>
<a href="force_download.php?file=ok.txt"> download ok.txt</a>