在PHP中使用fopen下载文件时出现问题


Trouble in downloading file with fopen in PHP

我想用fopen下载一个sql文件,我的问题是下面的代码除了像这样的html文件外,还会下载整个sql文件:

<!DOCTYPE html>
...
<span style='"font-family:tahoma,arial,helvetica,sans-serif'">sth</span></p>
'n","media/images/","media/images/","0","en","1","0","2000-11-30","admin","sth","0","2000-12-13 00:00:00","0","","","","","12","","","","0","","73","1","1","1","1","1","1","1","1","1","1","1","1","1","0","0","0","","","","","0","");
INSERT INTO sctp_contents VALUES("458","","books ","","<html>

代码:

header("Content-type: " . 'application/octet-stream');
header("Content-Disposition: attachment; filename=/backup/".$db_bkup);
header("Content-Transfer-Encoding: binary");
header("Content-length: $file_size");
while(!feof($handle)) {
    echo fread($handle, $file_size);
    flush();
}
fclose($handle);
die;

帮帮我,谢谢

将其余代码拼凑成可读格式后,我看不到任何特别的错误——对我来说,唯一看起来奇怪的是jDateTime~这可能是PHP后期版本中的一个函数,也可能是我不知道的自定义函数,但它看起来很奇怪。

除此之外,在开始下载并使用正确的标题之前,可以使用ob_clean来确保没有字符输出。

ob_clean();
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=/backup/{$db_bkup}");
header("Content-Transfer-Encoding: binary");
header("Content-Length: {$file_size}");
while( !feof( $handle ) ) {
    echo fread( $handle, $file_size );
    flush();
}
fclose( $handle );
die;