1

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;

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.