开发者

Forcing download in Safari keeps window open

开发者 https://www.devze.com 2023-02-26 02:46 出处:网络
I\'m forcing a download on links using this code from the PHP.net site: if (file_exists($file)) { header(\'Content-Description: File Transfer\');

I'm forcing a download on links using this code from the PHP.net site:

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, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}

I put this code in a file called 'download.php', and call it from a page (<a href="d开发者_JS百科ownload.php?file=abc.doc">Click</a>).

It works fine on all browsers, but on Safari the window it opens ('download.php') stays open during and after the download/open process. In other browsers it disappears immediately. Has anyone else come across this problem, and if so are there any suggestions as to how to solve it?


I tried this and it seems to be working equally in all browsers. Try this code, considering that Safari and IE need of more characters as headers to be sent:

#$file = 'load.php';
    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, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();

for($i = 0; $i < 40000; $i++) {
    echo ' '; // extra spaces
}
flush();
usleep(50000);
exit;
0

精彩评论

暂无评论...
验证码 换一张
取 消