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!
Related
I was searching already for a long time and I havent seen any right answer yet.
I'm trying to create a system in PHP where the user can download a signPicture that I create in JPG.
The program is working fine in all desktop computers. There is not problem at all, even for IE8.
The header that I use:
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="test.jpg"');
in the end i just stream the picture:
imagejpeg($imgSign,NULL,100);
How I said, it's working really good in every browser. But then we get to the mobile devices, where in android for example, download a test.jpg file... but then it cannot open... and the same with ipad (actually doesnt download, it show the image in the browser and than I save it... but it does not open either).
I also try more examples that I saw, but doesnt change anything, like:
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/download");
header("Content-Transfer-Encoding: binary ");
Any idea how to sort this out in mobile devices?
Thanks!
I got it!
There were differents problems. I found the clear solution in comments from this post:
http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="test.JPG"');
The important steps: I send everything with a form. The form, to make it work in mobiles, needs to have the target='_top' and the method='get'
It also make errors if the extention (jpg) is not in UPPERCASE and the file name is not between " ".
Now it works in all devices that I try by far. :)
Special thanks to Jörg Wagner, author of the post.
I am selling a digital product and want to hide the true location of the download.
So I'm using a redirect script like this:
protected function redirectDownload ($realfilename) {
ob_start();
$mm_type="application/octet-stream";
header("Cache-Control: public, must-revalidate");
header("Pragma: no-cache");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($realfilename)) );
header('Content-Disposition: attachment; filename="'.$this->fakefilename.'"');
header("Content-Transfer-Encoding: binary\n");
ob_end_clean();
readfile($realfilename);
}
The zip file is always corrupted when I download it, but when I download it directly it is fine.
Does anyone know why this might be?
I think this was working fine on another server, but would need to confirm that.
If I can't solve this, is there any other techniques or services I can use to do this?
Open the downloaded(corrupted) file in an text-editor, i guess there has already been some output before you call the function.
You should better use ob_start() at the begin of your script instead of the begin of the function.
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.
i am using php_excel to export to xlsx. In my application im making use of template.
When i download, the xlsx file gets downloaded fine, but when we open its showing the following warning:
"Excel found unreadable content in 'project_report(3).xlsx'. Do you want to recover the contente of this workbook? If you trust the source of this workbook, click Yes."
If i click yes it opens the file correctly.
And one more thing is when i attach the downloaded file to the mail. And if i open it sing Google Spread sheet it says bad format unable to open.
so if anyone know the reason please suggest me to solve this.
check your headers, here are mine:
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-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
$objWriter->save('php://output');
check for output (spaces before <?php forgotten echo? etc
Normally, when I want to allow a user to download a file without revealing the exact location, I just use something like this to let them download the file:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . $filename) . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($filename));
readfile("$filename");
But if they are using a modern browser or other download client, and they pause the download and try to resume it, the script (assuming they are still authenticated or whatever) will resend the headers and the file contents from the beginning, thus breaking the download, and basically requiring the file to be redownloaded from the beginning.
How can I enable my script to compensate for paused (and consequentially, resumed) downloads?
Use php's built-in fopen to open the file and then fseek to the right place (based on the range in the request header) and then return the partial file using fpassthru instead of using readfile.
You can find some example code in php under the comments for fread
You need to read the request headers like Range, If-Range, etc then seek to the correct location in the file. Normally a web-server would do this for you on an ordinary file. It's a bit complex but here's something that might get you started:
http://forums.asp.net/t/1218116.aspx
http://www.notes411.com/dominosource/tips.nsf/0/480C4E3BE825F69D802571BC007D5AC9!opendocument
For the second link the code is in part 12