SimonC
SimonC

Reputation: 1618

Firefox downloads retrieve.php instead of filename given per Content-Disposition header

I have a PHP script called retrieve.php that compresses uploaded files from a directory in to a .zip file and then outputs them to the stream, so said file can be downloaded.

The script works in all browsers except Firefox. After searching online for quite a while now and coming across this question: PHP download script returns download-file.php instead of filename and still having the same issue, I'm stumped and have no clue what's going wrong.

This is the code being used to download the file:

if ($zippedElements >= 1) {
//      die($zipFilename);
        $zipFilename = "download-".time().".zip";
        // Send zip to recipient
        header("Content-Dispositon: attachment; filename=\"$zipFilename\"");
        header("Content-Length: ".filesize($zipFileName));
        header("Content-Type: application/zip");
        header("Content-Transfer-Encoding: binary");
        header("Connection: Keep-Alive");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: public");
        header("Accept-Ranges: bytes");
        readfile($zipFileName);

    } else return;

I've tried setting the header to different values, such as content-type: application/force-download, adding the asterisk (*) after filename, etc., but all to no avail.

The result in Firefox looks as follows:

enter image description here

Any help with the matter would be much appreciated.

Upvotes: 0

Views: 60

Answers (1)

user2182349
user2182349

Reputation: 9782

You have inconsistencies in the variable name $zipFilename - the N is uppercased sometimes.

    $zipFilename = __FILE__;
    // Send zip to recipient
    header("Content-Dispositon: attachment; filename=\"$zipFilename\"");
    header("Content-Length: ".filesize($zipFilename));
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");
    header("Connection: Keep-Alive");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: public");
    header("Accept-Ranges: bytes");
    readfile($zipFilename);

Upvotes: 1

Related Questions