Php exec and return binary - php

I am trying to run the following in php
$test = svn cat ....
Now the output of $test is basically a binary file returned by svn. How do I make this binary file available as a download. Am trying to put the following:
$test = `svn cat ....`
header("Content-Disposition: attachment; filename=" . urlencode($filename));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
echo $test;

From the comments on PHP.net's documentation on passthru():
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"myfile.zip\"");
header("Content-Length: 11111");
passthru("cat myfile.zip",$err);
exit();
The above code was provided by igor at bboy dot ru.

Have you considered saving this file to temporary location on the hard disk and serving it from there? Is it really necessary to serve the file from memory? What if you have 500 people downloading this file. Will the server save all 500 files in memory while the users are downloading them?
My recommendation, save the file to a temporary location that is accessible to your web server and give them a link.

In addition to Peter D's answer; you could write the binary file to the file system and then serve it as a download. Instead of giving users a link.
Try it with a simple text file first, if that works; try it with your binary file.

You probably want to use the passthru() function in PHP.
The call might need to come after the headers, but try both ways first.
edit: I don't think this will cause a memory issue. I don't think PHP will keep the output in memory, because it's sent straight on through to stdout.

The problem might be that you've got three content-type headers - as far as I know, browsers only accept one so I'd go for octet-stream.

Related

Rename a file with PHP right before it downloads without saving it to the server?

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"');

Force download file that's not placed on the server

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.

Zip file downloads, but is invalid?

I use this code to enable users to download a zip file:
if(file_exists($filename)){
header("Content-Disposition: attachment; filename=".basename(str_replace(' ', '_', $filename)));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($filename));
flush();
$fp = fopen($filename, "r");
while (!feof($fp))
{
echo fread($fp, 65536);
flush();
}
fclose($fp);
exit;
}
When the file is downloaded, it only downloads 25,632 kilobytes of data. However the zip file is 26,252 kilobytes ...
Why does the browser get all 25MB but then stop?
I checked the Content-Length header to make sure it was correct and it is...
edit
In firefox, when i download the file, it says 'of 25mb' SO the browser thinks that 25mb is the COMPLETE amount... however, the content-length when echo'd is 26252904?
add this before your code
ob_clean();
ob_end_flush();
Your header('Content-Type ...) calls are useless as only the last one will be sent to the browser.
Downloads are triggered by Content-Disposition: attachment. You should send the actual Content-Type: application/zip if you are sending a zip file.
Finally, your read loop is unnecessary.
Putting it all together, your code should look like this:
if (file_exists($filename)) {
$quoted_filename = basename(addcslashes($filename, "\0..\37\"\177"));
header("Content-Disposition: attachment; filename=\"{$quoted_filename}\"");
header('Content-Type: application/zip');
header('Content-Length: '.filesize($filename));
readfile($filename);
}
Use a single MIME type to represent the data.
In this case using application/octet-stream will do just fine. This is when you dont know the MIME before hand. When you know it, you must put it. Do not use multiple content-type headers.
Usually, when the browser doesn't know how to handle a particular MIME, it will trigger the download process. Further, using Content-disposition: Attachment; .. ensures it.
There exists a simple readfile($filename) which will send out the bytes of the file to the requesting process like below:
header("Content-disposition: attachment;filename=" . basename($filename);
readfile($filename);
I had similar problem. The file downloaded fine in Firefox but not in IE. It appeared that Apache was gzipping the files and IE was not able to ungzip so the files were corrupted. The solution was to disable gzipping in Apache. You can also check if PHP is not gzipping on the fly and disable it too.
For Apache you can try:
SetEnv no-gzip 1
And for PHP, in .htaccess:
php_flag zlib.output_compression on
This answer is by No means a REAL ANSWER.
However i did get it to work... I just set the Content-Length to 30000000. Therefor it thinks the file is bigger than it actually is, and then it downloads it all.
Ugly hack i know, but i couldn't find ANY other way

PHP File Download

I'm currently building a script that will allow a user to download a file via a URL without actually seeing the filename or where the file is stored. So far I have everything built out, but I need to know how I would go about calling the file to open and download. I currently have a working version (code below), but for some reason the PHP is corrupting the download. Everytime I try to open a file that downloads to my desktop I get a corrupt error message. When I open the same file on the server itself, the file works just fine.
URL Structure:
http://www.example.com/download/file/cjVQv0ng0zr2
Code that initiates the download
$fullpath = BASE_PATH . '../uploads/brochures/' . $vendors['0']['filename'];
header("Content-type: application/pdf");
header('Content-disposition: attachment; filename="' . $fullpath . '"');
Am I doing something wrong that would cause the file to become corrupt? Am I missing a header or two?
Thanks in advance,
Jake
You need to call the following line after sending the header.
readfile($fullpath);
and also adjust in the header like this:
header('Content-disposition: attachment; filename="' . basename($fullpath) . '"');
One thing i am not sure about is the $fullpath .. try to see if the $fullpath you have is correct and you can actually reach the file, this needs to be the full physical path of the file.
I think it would also be a good idea to add the following header as well:
header("Content-Transfer-Encoding: binary");
I had a similar issue a while back. Make sure you don't have any extra whitespace in your script file, either before the "<?php" tag or after the "?>" tag. In my case the last character of my script was "\n" instead of the expected ">".
I had faced the same problem sometime back, following worked for me; put a
while( #ob_end_clean() );
just before header functions:
header("Content-Type: ". $row['p_mime']);
header("Content-Length: ". $row['p_size']);
header("Content-Disposition: inline; filename=".$row["p_name"]);
Content-disposition: attachment/inline has to be set according to cases (1. prompt for download / 2. open in browser)
NOTE: Take care that you are not echoing and value before the header function, and being over cautious will not do any harm, silent out all the function before header function which you think would fail or spawn a warning message prefixing "#" symbol to those lines of php code.
all the best :)
Make sure you exit...
(i'm using a blob)
header("Content-Type: " . $response['content_type'] );
header("Cache-Control: maxage=1");
header("Pragma: public"); //fixes ie bug
echo trim($_data);
exit();

PHP generated Excel file is different when downloaded

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.

Categories