/    Sign up×
Community /Pin to ProfileBookmark

Header Question

Using headers to force a download with “Content-type: application/force-download”, and providing the web browser with the filename by using “Content-Disposition: attachment; filename=$filename”, is there some way to tell the browser what the file’s size is? That way, when downloading a file, I could actually see the percent of it that’s been completed, instead of guessing.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderJan 05.2007 — [code=php]
header("Content-Length: ".filesize("/path/to/my/file/filename.pdf"));
[/code]


Or if you are not using a file, for example if you are generating a thumbnail of an image on the fly.

[code=php]
ob_start();
// ECHO THE DATA HERE
echo file_get_contents("/path/to/my/file/filename.pdf");
header("Content-Length: ".ob_get_length());
ob_end_clean();
[/code]


The above should work ok, just typed from memory..
Copy linkTweet thisAlerts:
@NightShift58Jan 05.2007 — Or this:[code=php]header("Content-Length: " . getimagesize($filename));[/code]
Copy linkTweet thisAlerts:
@AmazingAntauthorJan 05.2007 — Ok, so the point is to use this general idea:
[code=php]
header("Content-Length: ".$filesize);
[/code]


Thanks to the both of you!
Copy linkTweet thisAlerts:
@MrCoderJan 05.2007 — Or this:[code=php]header("Content-Length: " . getimagesize($filename));[/code][/QUOTE]

Wouldnt that return an array on anything that was not a URL or MIME file?
Copy linkTweet thisAlerts:
@NightShift58Jan 06.2007 — You caught me out there in left field... I was working on another (image) script and I have to assume I got distracted in the midst of Copy&Paste.

In fact, I think it returns an array not matter what and that doesn't even contain the size, as such.
×

Success!

Help @AmazingAnt 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.19,
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,
)...