2

I have a script to download a dump of my database, but the file is getting large. I tried compressing it with :

$dump = `mysqldump -u $username -p$password $dbname`;
$fp = fopen('php://temp', 'r+');
stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, array('level' => 9));
fputs($fp, $dump);
rewind($fp);

//Envoi du "fichier"
$this->setLayout(false);
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: SQL Dump");  
header('Content-Disposition: attachment; filename="mydump.sql.zip"'); 
$this->fichier = stream_get_contents($fp);

But that creates an invalid zip file. Am I missing something ?

EDIT

There must be somthing wrong in the headers too, Firefox shows the file as "HTM document", and I can't show a filesize.

Manu
  • 4,078
  • 4
  • 37
  • 74
  • `zlib.deflate` will do that compression, however it does not turn it into a zip file (which has some headers, a directory listing, an optional comment and such stuff like CRC32 checksums) – hakre Oct 26 '12 at 14:49
  • I was thinking about that, but can I add those headers ? – Manu Oct 26 '12 at 14:50
  • That are file-headers *not* HTTP headers. Technically you can, but I'm pretty sure you don't want to ;) – hakre Oct 26 '12 at 14:50
  • I know they're not the same :) I thought there was a function to add them though. Oh well. – Manu Oct 26 '12 at 14:53
  • Well, checkout this question then: [Zip Stream in PHP](http://stackoverflow.com/q/3078266/367456) – hakre Oct 26 '12 at 14:55

1 Answers1

8

why not simply do

$dump = `mysqldump ... | gzip -9 > somefile.gz`;

and skip the entire "do gzip inside php"?

You could even replace the entire bit with

passthru('mysqldump ... | gzip -9');

and dump the output directly to the client browser.

Marc B
  • 340,537
  • 37
  • 382
  • 468
  • Smart. I was trying out stream compression so I didn't think of it :) – Manu Oct 26 '12 at 14:42
  • There must be somthing wrong in the headers too, even with "| gzip -9" the file cannot be openned – Manu Oct 26 '12 at 14:59
  • 1
    well, you've got 3 separate content headers, which is technically incorrect. You're basically saying "this data is an apple. it's also a giraffe, and it's also a football". A file can have only a single mime-type, and each subsequent content-type header simply replaces the previous one(s). download the file and open it in a hex editor (or event just notepad) and see if there's any garbage before the gzip data starts, e.g. php warnings. – Marc B Oct 26 '12 at 15:02
  • Oh actually no, now the file is simply empty. Ideas ? – Manu Oct 26 '12 at 20:43