0

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.

Community
  • 1
  • 1
Rhyzko
  • 13
  • 1
  • 5
  • 1
    If you are just getting the name of Firefox wrong, you should look in the developer tools. The Network tab will show the request and response for the PDF so you can see if there are any errors there. – Quentin Sep 14 '14 at 19:32
  • Sorry for the misunderstanding, I'm using Mozilla Firexfox v32.0.1 – Rhyzko Sep 14 '14 at 19:32
  • 1
    Why do you set the `Cache-Control` header twice? The second one will replace the first. – Alexander O'Mara Sep 14 '14 at 19:37
  • Also, your code works fine for me in Firefox 32.0.1. – Alexander O'Mara Sep 14 '14 at 19:40
  • Quentin, with the Network tab, I don't see anything :/ Alexander, I don't understand why it works for you and not for me :o – Rhyzko Sep 14 '14 at 19:47
  • Oh, I realized that the problem is not due to the download.php file, but my seems to be "inactivable" with Firefox. I still don't understand why :/ – Rhyzko Sep 14 '14 at 19:59
  • Sorry for the inconvenience, it was a bootstrap issue and I solved my problem with this post on StackOverflow [link](http://stackoverflow.com/questions/16280684/nesting-a-inside-button-doesnt-work-in-firefox) Should I delete my post ? – Rhyzko Sep 14 '14 at 20:10
  • Either delete it, or write at the top of your question that it is solved... not at the bottom of all the comments. – KIKO Software Sep 14 '14 at 22:14

2 Answers2

5
    $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);
Nishad Aliyar
  • 77
  • 1
  • 3
-2

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