/    Sign up×
Community /Pin to ProfileBookmark

Downloading a File That Was Made Viewable in Browser by Headers

Suppose there is a file that is displayed on the page using headers:

[CODE]header(“Content-Length: ” . filesize($file) );
header(“Content-type: application/pdf”);
header(“Content-disposition: inline”);
header(‘Expires: 0’);
header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0’);[/CODE]

How can I download that file externally, using curl or file_get_contents or in any other way?

I tried this but it downloads an empty file:

[CODE]$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url); //specify the url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$body = curl_exec($ch);

header(‘Content-Disposition: attachment; filename=”downloaded.pdf”‘);
header(“Content-Length: ” . strlen($body));
header(“Content-Type: application/octet-stream”);
echo $body;
[/CODE]

Can anyone help?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 23.2016 — I might try using the headers on this page: http://php.net/manual/en/function.readfile.php

Also, it's necessary that [i]nothing[/i] gets sent to the browser until all the header() commands are executed, so make sure there is nothing before the opening <?php tag, and that nothing else is getting echoed out in any way.
×

Success!

Help @Ivannnnn 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.18,
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,
)...