繁体   English   中英

PHP下载文件不适用于Firefox

[英]Php downloading file doesn't work with Firefox

已解决,由于已知的自举问题( 在<button>内嵌套<a>在Firefox中不起作用 )。

我正在尝试简单地强制下载文件,并且该文件在Chrome和Safari上工作正常,但在Firefox上却无法正常工作。

我已经下载了download.php文件来下载我的文件(在“ a href”中使用):

<?php

$filename="myFile.pdf";
$file="../content/$filename";
$len = filesize($file); // Calculate File Size
if (ob_get_contents()) ob_end_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Type:application/pdf"); // Send type of file
$header="Content-Disposition: attachment; filename=$filename;"; // Send File Name
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len); // Send File Size
@readfile($file);
exit;

?>

它用于:

<a class="myFileClass" href="download.php">Download</a>

因此,对于Chrome和Safari,当我单击下载链接时,文件即被下载! 但是,使用Firefox不会发生任何事情。

对这个奇怪的问题有什么想法吗?

预先感谢。

    $header="Content-Disposition: attachment; filename=$filename;";

这是错误的,请在文件名中使用引号。 它应该像下面

    header('Content-Disposition: attachment; filename="' . basename($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);

上面带有基本名称参考等的示例将下载一个空文件。

下面的代码返回包含html数据的数据,这些数据也包含在Wordpress调用中,用于输出,但仅清除Wordpress之外的数据。

这两个代码仅在IE中产生下载调用。

$filename = $db_record.'_'.date('Y-m-d').'.csv';
header('Pragma: public');
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the Past
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. $filename);
Readfile($filename);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM