user2824073
user2824073

Reputation: 2485

Unable to force download file with PHP

I need to create a PHP page which starts automatically the download a file (I don't want to expose the Download link). I've tried with several examples on the web but all examples will end up with opening the file in the browser with the uncorrect content type. For example:

 <?php
// We'll be outputting a ZIP
header("Content-type: application/zip");

// Use Content-Disposition to force a save dialog.
// The file will be called "downloaded.zip"
header("Content-Disposition: attachment; filename=downloaded.zip");

readfile('downloaded.zip');
?>

When I execute this page, the output on the browser is: enter image description here After trying all possible variants of this example my idea is that the problem is with my hosting environment. Which variable should I check and maybe ask to be enabled to my provider ?
Thanks!

Upvotes: 0

Views: 106

Answers (2)

Fabian Schneider
Fabian Schneider

Reputation: 365

Here's the code I used back in the day:

                        header ("Content-Type: application");
                        header ("Content-Disposition: attachment; filename=\"$file\"");
                        header ("Content-Length: " . filesize ('./FILES/' . $file));
                        header ("Cache-Control: no-cache");
                        header ("Pragma: no-cache");
                        readfile ('./FILES/' . $file);
Just modify the content-type header ofc.

Upvotes: 0

Francesco Marchioni
Francesco Marchioni

Reputation: 4338

Try printing out the following variable using phpinfo(): SERVER["HTTP_ACCEPT_ENCODING"]. My suspect is that you don't have zip allowed- could be something different like "gzip" for example.

Upvotes: 1

Related Questions