|
Posted by Andy Jeffries on 10/30/04 11:48
On Mon, 22 May 2006 22:02:51 +0000, Paul F. Johnson wrote:
> $command = "zip -9 " . $fn[$id] . ".zip " . $files;
> header("content-type:application/zip");
> header("Content-disposition: attachment; filename=" . $fn[$id] . ".zip");
> header("Pragma: no-cache");
> header("Expires: 0");
> shell_exec($command);
>
> The files do exist and a save as window appears, but the file generated
> is 0 bytes long. I have tried changing the command to "/usr/bin/zip" and
> "//usr//bin//zip" and putting the shell_exec before the header lines as
> well changing shell_exec to exec and even just system. All the same
> result.
>
> Is there something I'm missing here?
Yes, re-read the help page for shell_exec, you're missing something there:
http://uk.php.net/shell_exec
Basically, shell_exec executes the command and returns the output as a
string. So what you are doing is running (for example):
zip -9 a.zip 1.pdf 2.pdf 3.pdf
When you run this command manually on the server, what output does it
produce? The answer is nothing (assuming the command succeeds as good
Linux utilities should). So, that's part one of the problem, there's no
output to actually send to the browser.
Part two of the problem is that even if you could get zip to write it's
output to "stdout" so you could then get the returned zip data from
shell_exec, you aren't actually sending it to the browser (i.e. you run
shell_exec without capturing the result or printing it).
What I would do is change your code to be as follows:
$command = "zip -9 $fn[$id].zip $files";
header("content-type:application/zip");
header("Content-disposition: attachment; filename=$fn[$id].zip");
header("Pragma: no-cache");
header("Expires: 0");
shell_exec($command);
@readfile("$fn[$id].zip");
In this, all I've done is change a few bits to make use of variable
substitution within a string (concatenation is faster but looks a lot less
readable) and then added a call to readfile at the end, which reads a file
from disk and prints the contents to the browser.
Cheers,
Andy
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos
Navigation:
[Reply to this message]
|