/    Sign up×
Community /Pin to ProfileBookmark

Force download

Is there a way to force a download, prompt a save to download, or open in new window when a button is clicked or when a page is loaded?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 06.2009 — You can send the following headers:
[code=php]
<?php
$file = '/file/you/want/downloaded';
header('Content-Length: '.filesize($file));
header('Content-Type: application/octet-stream'); // change this if you know exact mimie type
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($file);
exit;
[/code]
Copy linkTweet thisAlerts:
@SyCoJan 06.2009 — You can use a hidden iframe so the user clicks the link, which is targeted to the iframe with perhaps an id to identify the file. The iframe runs the above code and the prompt appears. It's a bit neater then a new window.
Copy linkTweet thisAlerts:
@walker6o9authorJan 06.2009 — I'm having the following problem with that code:

Warning: Cannot modify header information - headers already sent by (output started at C:wampwwwannualReportindex.php:72) in C:wampwwwannualReportindex.php on line 278

Warning: Cannot modify header information - headers already sent by (output started at C:wampwwwannualReportindex.php:72) in C:wampwwwannualReportindex.php on line 279

Warning: Cannot modify header information - headers already sent by (output started at C:wampwwwannualReportindex.php:72) in C:wampwwwannualReportindex.php on line 280
Copy linkTweet thisAlerts:
@SyCoJan 06.2009 — headers have to be the very first thing output to the browser. Not even a space can go before.
Copy linkTweet thisAlerts:
@walker6o9authorJan 06.2009 — Is there another way of doing this then?
Copy linkTweet thisAlerts:
@SyCoJan 06.2009 — if you link to an iframe you don't need any output, just the PHP
Copy linkTweet thisAlerts:
@walker6o9authorJan 06.2009 — How would I link to an iframe?
Copy linkTweet thisAlerts:
@SyCoJan 06.2009 — You set the target on a link as the frame name. Google for an iframe tutorial. It's not too tricky.
Copy linkTweet thisAlerts:
@NogDogJan 07.2009 — Note that, iframe or not, the link needs to point to a server-side script whose only responsibility is to supply the download (as per the code in my first reply); you cannot simply put the download code in the middle of a script that is doing other outputs (such as the HTML page display).

main_page.php:
[code=php]
<?php /* this is the main page */ ?>
<p>blah blah blah
<a href="download_file.php">Download file</a>
blah blah blah</p>
[/code]

download_file.php:
[code=php]
<?php
/* This file only does the download, nothing else. */
$file = '/file/you/want/downloaded';
header('Content-Length: '.filesize($file));
header('Content-Type: application/octet-stream'); // change this if you know exact mimie type
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
readfile($file);
exit;
[/code]
×

Success!

Help @walker6o9 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...