1 $filename = "http://www.xxxx.com/images/xiaoxiangmuban.jpg"; 2 $fb = @fopen($filename, "rb+"); 3 if($fb){ 4 $fileinfo = pathinfo($filename); 5 header('Content-type: application/x-'.$fileinfo['extension']); 6 header('Content-Transfer-Encoding:binary'); //二进制 7 header('Content-Disposition: attachment; filename='.$fileinfo['basename']); 8 header('Content-Length: '.filesize($filename)); 9 ob_clean (); 10 flush (); 11 readfile($filename); 12 fclose($fb); 13 exit(); 14 }else{ 15 die("文件不存在"); 16 }
之前搞了比较久,突然发现代码上部分写了一个header("Content-type:text/html;charset=gbk"),然后后来在大神的执导下用了一个ob_clean()和flush(),然后可以成功下载。