PS:這段代碼在win系統下跑沒有問題,楓哥親測可用.如果用的是linux或是其他系統,要要諸位自行測試下啦.
$filename = "./".$_REQUEST['name']; //這裡是楓哥具體路徑,只要$filename是所要下載文件的完整路徑就可以. if (!is_file($filename)) {//檢測文件是否存在. die('下載的文件好像跟TM誰私奔啦!'); } $filepath = str_replace('\\', '/', realpath($filename)); $filesize = filesize($filepath); $filename = substr(strrchr('/'.$filepath, '/'), 1); $extension = strtolower(substr(strrchr($filepath, '.'), 1)); // use this unless you want to find the mime type based on extension,文件後綴格式,不解釋. $mime = array('application/octet-stream'); header('Content-Type: '.$mime); header('Content-Disposition: attachment; filename="'.$filename.'"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: '.sprintf('%d', $filesize)); header('Expires: 0'); // check for IE only headers,坑爹的IE檢測,你懂的. if (isset($_SERVER['HTTP_USER_AGENT']) &&((strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false))) { header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); } else { header('Pragma: no-cache'); } $handle = fopen($filepath, 'rb'); fpassthru($handle); fclose($handle);
這個代碼是楓哥從國外的一個站點上找到的,親測可用.度娘出來的結果,你懂的.因此在博客上共享出來,希望對大家有用.國外的站,地址忘了,沒有辦法放出,理解下.最後,嚴肅的聲明下:這個代碼不是我寫的,是轉載的.