...

View Full Version : header() not allowing me to download the format



SimpleNE
01-16-2012, 05:35 PM
I get an error everytime I run this script. It isn't pushing the file for download / opening it no matter what I do. I keep getting errors... But when I take off the header() it spits the byte to me. Ideas?



<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.mainwebsitelink.com/Download.do?action=download&dataId=55555&format=title_of_the_file_i_want');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
echo $contents;

$file = $contents;

header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($file));

header('Content-Disposition: inline; filename=' . basename($file));
// header('Content-Disposition: attachment; filename=' . basename($file));
readfile($file);/
?>

BluePanther
01-16-2012, 05:40 PM
By echo'ing the contents before the error's, you're sending the headers before your header() statement by default. Headers are sent BEFORE output, so having output before produces an error (which you would see if error reporting is on).

Remove that echo and everything will work - look up header() in the php manual and that will tell you more.

SimpleNE
01-16-2012, 05:59 PM
I did so and still get an error. The image is attached when I run the script. Here is the new version of the script. By the way, thanks for that catch, one step closer!



<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.mainwebsitelink.com/Download.do?action=download&dataId=55555&format=title_of_the_file_i_want');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);

$file = $contents;

header('Content-Description: File Transfer');
header('Content-Type: application/pdf');
header('Content-Length: ' . filesize($file));

header('Content-Disposition: inline; filename=' . basename($file));
// header('Content-Disposition: attachment; filename=' . basename($file));
readfile($file);
?>



I should mention that when I use the code below that if I do a "save page as" and I finish it off with a .pdf, the file is perfect! I don't know if that helps this issue or not but wanted to mention, here is that code:


<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.getabstract.com/Download.do?action=download&dataId=15054&format=1&u=us_department_of_veterans_affairs');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$contents = curl_exec ($ch);
curl_close ($ch);
echo $contents;
?>

BluePanther
01-16-2012, 06:09 PM
Wow, that was some terrible English. What I meant to say was, you're using $contents as a filename but it looks more like it's the actual pdf contents. Is $contents a filename/location, or pdf contents?

SimpleNE
01-16-2012, 06:14 PM
No problem... Thanks again for the help! It is pdf content...

Fou-Lu
01-16-2012, 07:17 PM
$contents isn't a file, you cannot expect to get the data using a readfile.
First specify the filename within the content-disposition. There is no "basename" of the string you have provided. Since $content is already the data you need, you simply print it.

SimpleNE
01-16-2012, 07:31 PM
$contents isn't a file, you cannot expect to get the data using a readfile.
First specify the filename within the content-disposition. There is no "basename" of the string you have provided. Since $content is already the data you need, you simply print it.

Thanks for the "basename" issue, I didn't even think of that. And when I print it / echo it back, I get the file in bit but I instead want it to save itself out as a PDF. Right now, I have to go to save page as and change the extension to ".pdf" to have it save correctly when I do the code above two posts.

How would you print it out as a pdf?

tangoforce
01-16-2012, 08:50 PM
First you need to know the mime type so code similar to this would help:


switch($file_extension)
{
case 'exe': $ctype='application/octet-stream'; break;
case 'zip': $ctype='application/zip'; break;
case 'mp3': $ctype='audio/mpeg'; break;
case 'mpg': $ctype='video/mpeg'; break;
case 'avi': $ctype='video/x-msvideo'; break;
case 'rar': $ctype='application/rar'; break;
default: $ctype='application/force-download';
}


Then you need to supply some details to the browser such as the length, the mime/content type and the attachments filename:



header('Content-Type: ' . $ctype);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Length: ' .$FileLength);


Note that (as demonstrated in one of the links in my signature) any call to header() must be BEFORE any other content is put out to the browser.

Fou-Lu
01-16-2012, 09:52 PM
How would you print it out as a pdf?

print $contents;.
You've already told the browser that you are presenting the data and want it provided as an attachment. The browser should prompt the user for a location to save the document.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum