简体   繁体   中英

Php downloading file doesn't work with Firefox

SOLVED, due to a bootstrap known issue ( Nesting <a> inside <button> doesn't work in Firefox ).

I'm trying to simply force a download of a file and it works fine on Chrome & Safari, but not on Firefox.

I've download.php file to download my file (used in a "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;

?>

It's used with :

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

So with Chrome & Safari, when I click on the download link, the file is downloaded ! But with Firefox, nothing happens.

Any idea of this curious issue ?

Thanks by advance.

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

This is wrong, please use quotes for file name. It should be like below

    header('Content-Disposition: attachment; filename="' . basename($file).'"');

Example code

    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);

The example above with basename reference etc. downloads an empty file.

The code below returns data with html data also included in Wordpress call for output but clean data only outside of Wordpress.

both code only produces download call in 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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