最近做的一个系统,需要下载doc文件
以前的代码下载完成后,打开总是乱码...
google咯很久也没有解决办法
后面总算搞定
必须在Header之前进行清除,即 ob_end_clean()
$file_size = filesize($logName); ob_end_clean(); header("Content-type:application/octet-stream"); header("Accept-Ranges:bytes"); header("Accept-Length:$file_size"); header("Content-Disposition:attachment;filename=" . $fileName); $fp = fopen($logName, "r"); $buffer_size = 1024; $cur_pos = 0; while (!feof($fp) && $file_size - $cur_pos > $buffer_size) { $buffer = fread($fp, $buffer_size); echo$buffer; $cur_pos+=$buffer_size; } $buffer = fread($fp, $file_size - $cur_pos); echo$buffer; fclose($fp); return true;
详情参照这个页面http://tilt.lib.tsinghua.edu.cn/node/610