I'm stuck...
For some reason no matter what I try, I can't get the browser to prompt me to download a file. I've taken the code below down to the minimum to try and troubleshoot it. What am I missing? When I run this, no prompt is given. When I view the page, it looks like it read the file, its just a bunch of unreadable characters.
<?php
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename='downloaded.pdf'");
readfile("docs/contract.pdf");
?>
Try using the original filename in Content Disposition header.You could try code like this:
<?php
header("Content-type: application/pdf");
header("Content-Transfer-Encoding: Binary");
header("Content-Disposition: attachment; filename=contract.pdf");
readfile("docs/contract.pdf");
?>
Also, Ensure no output before sending headers. Sending/modifying HTTP headers must be invoked before any output is made. Otherwise the call fails.
Related
I need to create a PHP page which starts automatically the download a file (I don't want to expose the Download link). I've tried with several examples on the web but all examples will end up with opening the file in the browser with the uncorrect content type.
For example:
<?php
// We'll be outputting a ZIP
header("Content-type: application/zip");
// Use Content-Disposition to force a save dialog.
// The file will be called "downloaded.zip"
header("Content-Disposition: attachment; filename=downloaded.zip");
readfile('downloaded.zip');
?>
When I execute this page, the output on the browser is:
After trying all possible variants of this example my idea is that the problem is with my hosting environment. Which variable should I check and maybe ask to be enabled to my provider ?
Thanks!
Try printing out the following variable using phpinfo(): SERVER["HTTP_ACCEPT_ENCODING"].
My suspect is that you don't have zip allowed- could be something different like "gzip" for example.
Here's the code I used back in the day: header ("Content-Type: application");
header ("Content-Disposition: attachment; filename=\"$file\"");
header ("Content-Length: " . filesize ('./FILES/' . $file));
header ("Cache-Control: no-cache");
header ("Pragma: no-cache");
readfile ('./FILES/' . $file);
Just modify the content-type header ofc.
I have a script that generates a pdf using fpdf, this file is saved correctly on the server on my computer. In that same php file I run the following code to download the file. When i download the pdf and I check it in notepad, everything in the pdf is the same, except for the fact that it contains a lot of my previous files html at the beginning of it. The file saved to my server doesn't have any of that.
What could cause something like this to happen? I have no idea where to look for the source of this error, can anyone point me in the direction to finding the problem?
<?php
$filename=($name.$ran.'.pdf');
$pdf->Output($name.$ran.'.pdf');
header ("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/octetstream');
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($filename));
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
readfile($name.$ran.'.pdf');
?>
I managed to figure things out, thanks to hakre! Essentially I just needed to clear out the output buffer using ob_clean () before using readfile() and the code worked!
I am making a call(PHP server) to an API and I can confirm that I am getting a response, my code at the point where I know I am getting a response is:
$binaryF = $rsObject->makeCall('get', "/JobData/{$_GET["jID"]}", "?format=bin");
//header("Pragma: public"); // required
//header("Content-Type: application/zip");
//header("Content-Disposition: attachment; filename=ss.zip");
//header("Content-Length: " . filesize($binaryF.length));
//header("Content-Transfer-Encoding: binary");
file_put_contents('C:\ss.zip', $binaryF);
If I keep the code as is and click on the link that takes me to the page with this code on it, ss.zip is created and I open it and confirm that it has the correct content. The data is coming from an API call on the first line and is basically a zip package. If I remove the comments and comment out the file_put_contents line then the browser opens a file save dialog box but if I save it the archive is 0 bytes?
How do I send the content to a browser after retrieving it from the api call? I do not want to save it to disk first, I want to send it to the browser making the request directly.
Thank you
Jack
Ok. I changed it to:
header("Pragma: public"); // required
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=ss.zip");
header("Content-Length: " . strlen($binaryF));
header("Content-Transfer-Encoding: binary");
and now it opens the file save. I save it and it is 8 K but when I try t oopen it I get a message of: "Windows cannot open the folder" It complains that the zip is invalid? At least I am getting somewhere, thanks for the help so far!
Try (untested):
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename=ss.zip');
echo $binaryF;
$binaryF would need to be a string value for the filepath to the file you are trying to serve up in order for filesize() to work.
You can use strlen() to get the filesize for use in the ContentLength header.
You of course still need to actually output the file to the client browser.
I had some similar problems with PDF's, for me the Content-Length couldn't be calculated correctly with strlen() because it doesn't support multibyte-characters.
instead try this:
header("Content-Length: " . mb_strlen($binaryF));
See the documentation for more detailed information: http://www.php.net/manual/en/function.mb-strlen.php
Ok, #zerkms helped me by pointing out that I should use: strlen($binaryF) instead of filesize($binaryF.length) He helped me in a chat session by telling me to open the zip archive(with something simple like notepad) that I did get with file_put_contents('C:\ss.zip', $binaryF); This showed that what was produced was only partially correct, there was some HTML content that slipped into the response of the actual call. So all credit goes to #zerkms, thanks.
What worked? the following worked for me:
header("Pragma: public"); // required
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=ss.zip");
header("Content-Length: " . filesize($binaryF.length));
header("Content-Transfer-Encoding: binary");
echo $binaryF
I need to retrieve our reports from the jasperserver report engine as a PDF, then I want the PDF to be forced as a download, instead of being displayed inthe browser. The problem with displaying in the browser is we don't want the report parameters to be displayed to the end users in the url.
If I enter this URL path into the browser I get a PDF document that shows in the same browser window with all the report data:
https://mysite.com:8443/jasperserver/flow.html?_flowId=viewReportFlow&reportUnit=sample_report&output=pdf;
What I would prefer to have happen is for a download dialog box to be used and for the users to download the PDF to their computer, instead of it showing in the browser.
I've tried the following php code, but can't get it to work. I get a return value of false, but nothing in the server logs that shows an error.
ob_start();
header("Location: $src"); /* Redirect browser */
$report_contents = ob_get_contents();
ob_end_clean();
var_dump($report_contents);
I'm not really sure how to go about this...anyone got any ideas?
Thanks for the help.
You could buffer the file to the PHP server then output with force download:
header('Content-disposition: attachment; filename=huge_document.pdf');
header('Content-type: application/pdf');
readfile('https://mysite.com:8443/jasperserver/flow.html?_flowId=viewReportFlow&reportUnit=sample_report&output=pdf;');
See the notes about using readfile over an HTTP stream wrapper
http://php.net/manual/en/function.readfile.php
how about
$source=$url
header("Expires: 0");
header("Cache-Control: private");
header("Pragma: cache");
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
readfile($source);
exit();
I am trying to do something like Facebook's "Download" Photo link when viewing an album photo. Trying to avoid opening popups.
Any Javascript/jQuery/PHP method to do it?
I am aware of this: http://www.jtricks.com/bits/content_disposition.html
But I don't have control over the server configuration to do so.
Please let me know what is the best way to achieve this.
You can simply use the header function of PHP to set the Content-Disposition header, for example:
header("Content-Type: image/jpeg");
header("Content-Disposition: attachment; filename=\"".$name."\"");
header("Content-Transfer-Encoding: binary");
header("Accept-Ranges: none");
header("Content-Length: ".$length);
echo $data;
You don't need to access the server configuration. Just use header()—before any HTML output.
Example:
header('Content-Disposition: attachment; filename=example.pdf');
You can send a Content-Disposition header using PHP. No need to change the server configuration. So maybe something like this:
<?php
$file = 'folder/file.jpg';
header('Content-Disposition: attachment; filename='.basename($file));
readfile($file);
?>