PHP下载文件不适用于Firefox

0 php firefox download

已解决,由于已知的自举问题(在<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;

?>
Run Code Online (Sandbox Code Playgroud)

它用于:

<a class="myFileClass" href="download.php">Download</a>
Run Code Online (Sandbox Code Playgroud)

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

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

预先感谢。

小智 5

    $header="Content-Disposition: attachment; filename=$filename;";
Run Code Online (Sandbox Code Playgroud)

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

    header('Content-Disposition: attachment; filename="' . basename($file).'"');
Run Code Online (Sandbox Code Playgroud)

范例程式码

    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);
Run Code Online (Sandbox Code Playgroud)