tags:

views:

551

answers:

5

Hello,

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;
A: 

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.

adam
+1  A: 

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.

Peter D
+1  A: 

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.

MiRAGe
+1  A: 

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.

Matt
A: 

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.

Calvin