I'm trying to force download a pdf file that I'm generating. I don't need the pdf file to be actually saved on the server.
So when I generate my pdf file, I get the file content. I then encode it with base64. Now the problem is that I need to force download it. I've looked all over the web, but I haven't found any search results that tells me how to do this without the file actually being placed on the site.
I've tried the following code:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
header("Content-Length: ".filesize($pdffile));
readfile(base64_decode($pdffile));
But, it's giving me a corrupt pdf file, (1 kb). The actual file should be around 50kb.
Any ideas, as to what I can try?
readfile trying to output content from file, but you have only data string. Try this instead:
header("Content-type: application/pdf");
header("Content-disposition: attachment; filename=\"invoice.pdf\"");
echo base64_decode($pdffile);
I also suggest rename $pdffile to $pdfcontent for even better clarification.
Related
I'm trying to use readfile to download a file from the server.
The file definitely exists and when I use the URL in browser directly it downloads the correct file as predicted.
However when I do it with the following code, it download the file with the correct filename but it is completely blank in content, and zero bytyes.
I am wondering if this code is in fact incorrect and it is instead creating a new file, which of course would be blank.
function downloadFile($filename){
$downloadroot = 'http://my.url.co.uk/exports/'.$filename.'.csv';
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename='.$filename.'.csv');
header('Pragma: no-cache');
readfile("$downloadroot");
}
What would be the cause of this? Is there an alternative way to download files from a URL without having to create some sort of "save target as" link?
You could use file_get_contents() see http://php.net/file_get_contents for the full details.
Or if it's only csv's you're trying to read:
http://php.net/manual/en/function.fgetcsv.php
I have an Adobe Illustrator file (AI) that we currently have a link to on a website which then downloads the file to your computer.
The link looks something like this...
http://domain.com/crm/index.php?entryPoint=fileupload_download&id=22440435-e8ee-bd6f-7612-533b2cd7690f&field=fuaifile_c&type=D1_Designs
What I need to do is rename this file as it downloads.
So I am asking if it is possible to pass this download through another PHP file right before it downloads which would allow me to change the filename on the fly that the user downloads. I cannot change the filename on the server but when it downloads I would like to be able to add some ID numbers to the filename on the fly if this is possibble? Any ideas how to accomplish this without having to resave the image on the server with a new name?
What you are looking for is the Content-Disposition header, as specified in RFC 2183:
Content-Disposition: attachment; filename=example.ai
You can set this header using the PHP header() function.
It's ugly, and assumes these aren't "large" files that would exceed your memory_limit, but
$data = file_get_contents($original_url);
header('Content-disposition: attachment; filename="new name with id numbers');
header("Content-type: application/octet-stream");
echo $data;
You could always enhance this to do byte serving - suck 10k from original url, spit out 10k to user, etc...
Just set the Content-Disposition:
header('Content-Disposition: attachment; filename="downloaded.pdf"');
(Example taken from PHP docs: http://www.php.net/manual/en/function.header.php).
Adding id:
$id = generateIdFromSomewhere();
header('Content-Disposition: attachment; filename="downloaded'.$id.'.pdf"');
I know it's something easy, but I can't seem to find the solution.
I want to download a pdf from a folder. The pdf is in a folder named forms.
My script is in a different folder.
When I try to download the file with the following code:
header('Content-Disposition: attachment; filename="forms/form1976.pdf"');
The filename becomes: forms-form1976.pdf. That's not right, the filename should be: form1976.pdf. How do I enter the the correct folder first?
You should do something like this
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename=example.pdf');
readfile("/path/to/yourfile.pdf");
I am using the following code in a php document to force download of a pdf form since the submission works only after you have it on your local machine rather online.
It downloads the file ok but it corrupts it.
I can no longer open the pdf document.
<?php
$file_name = 'costumer.pdf';
$file_url = 'http://www.lopezi.com/forms/' . $file_name;
header('Content-Type: application/pdf');
header("Content-Transfer-Encoding: Binary");
header("Content-disposition: attachment; filename=\"".$file_name."\"");
readfile($file_url);
?>
The Content-Transfer-Encoding header shouldn't be needed in this case. Further I suspect that you have corruption in the outputted file.
Download it somewhere, open notepad, and drag the file in there. If any PHP warnings or errors were generated you will see them at the top.
Also, try to avoid the option of having more content return from the script, causing problems with the download, end with something like:
die(file_get_contents($file_url));
This way you cannot accidentally break the code easily by adding more output.
I have a PHP file that generates xls files using the module found at http://pear.php.net/package/Spreadsheet_Excel_Writer/
I can create the sample document just fine and when I open it, it looks fine.
My next step it to turn it into a downloadable link. To do that, I did this:
$mimeType = "application/vnd.ms-excel";
$file_name = "test.xls";
$file_path = "/tmp/".$file_name;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Type: application/' . $mimeType);
header('Content-Length: '.$size);
header("Content-Disposition: attachment;filename=$file_name ");
header("Content-Transfer-Encoding: binary ");
// open the file in binary read-only mode
// display the error messages if the file canĀ“t be opened
$file = & fopen($file_path, 'rb');
if ($file) {
// stream the file and exit the script when complete
fpassthru($file);
exit;
} else {
echo $err;
}
When I download the file however, it contains a lot of garbage data both in Excel and OpenOffice. The diff says that then binary file in the /tmp folder and the downloaded file are different from each other. I'm guessing that it has something to do with the headers or with fpassthru but I haven't had much luck with debugging the issue.
Any ideas on what the problem is?
The multiple Content-Type headers are uncessary. You're essentially saying that the file is a muffin and a pizza and a ford taurus all at the same time. All you need is the application/octet-stream version, unless you want to serve up the exact mime type.
As well, is there any reason you're trying to turn the file handle returned by fopen() into a reference?
Try something simpler:
<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment;filename=$file_name");
readfile("/tmp/test.xls");
exit();
?>
and see if that does any better.
Just make sure that you don't send ANYTHING out to the browser BEFORE the actual file content gets send.
It might just be some php 'error' or even 'notice' that Spreadsheet_Excel_Writer is producing and you don't even see. Or it might be a closing '?>' tag thats followed by s simple space or newline.
I had a similar error where the file that was generated inside the web folders were working. However the delivery using header('...') gave me corrupt files. This was due to a single space at the end of one php file after the closing '?>' tag.
I am using the same library and I just discovered that the files in the library itself are creating the whitespace.
Solution: In the following files remove the whitespace at the end of the file, or remove the ?> closing tag at the end.
Files to edit (all files in the Spreadsheet_Excel_Writer package):
Writer.php
Workbook.php
Worksheet.php
PPS.php
Parser.php
OLE.php
Parser.php
File.php
BIFFWriter.php
Validator.php
Root.php
Add the following code at the top of the page where the excel file is generated
ob_clean();
This would clear all the gibberish data.Also check for any echo statements.If echo statements are present, remove them. The data should always present in format specified by excel package.