Downloading a File That Was Made Viewable in Browser by Headers

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

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’);

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:

$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;

Can anyone help?

1 Like

You need the URL to the file. If you don’t have that, you can’t download it, because you or rather your script can’t locate it.

Scott

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.