2

I can directly download database dump using:

$filename = $database . '-' . date("d-m-Y_(G_i_s)") . ".sql.gz";
$mime = "application/x-gzip";


header("Content-Type: " . $mime);
header('Content-Disposition: attachment; filename="' . $filename . '"');


$cmd = "mysqldump --opt -h $host -u $username -p$password $database";


passthru($cmd);
exit(0);

Can I do it with .tar file make from directory? Just generate file and download it, without saving on server?

I can execute this (and file is correct):

exec('tar -czf /path/download/backup.tar.gz /path/download/');

But I don't want to store backup file on server... I want to download it directly. I tried to do something like my database dump example... but without success.

Any ideas how to do it?

EDIT:

Thanks for help. Removing -f was good idea.

Now it works:

$filename = 'download-' . date("d-m-Y_(G_i_s)") . ".tar.gz";
$mime = "application/x-tgz";


header("Content-Type: " . $mime);
header('Content-Disposition: attachment; filename="' . $filename . '"');
$cmd = "tar -cz " . $_SERVER['DOCUMENT_ROOT'] . "/download/";

passthru($cmd);
exit(0);
tshepang
  • 12,111
  • 21
  • 91
  • 136
M4ciek
  • 33
  • 9
  • Leave the `-f` filename flag out, then `tar` sends it directly to stdout, suitable for `passthru()`. – mario Nov 18 '13 at 21:55
  • possible duplicate of [Creating a GZipped tar file and dynamically streaming it via PHP?](http://stackoverflow.com/questions/10162759/creating-a-gzipped-tar-file-and-dynamically-streaming-it-via-php) – mario Nov 18 '13 at 21:59
  • OMG thanks :) removing -f was enough. One more question - how can I set directory structure in tar.gz file? Now I get full path with home/user/domains/domain.pl/public_html... I'd like to have only files from folder included in my command. – M4ciek Nov 18 '13 at 22:05
  • 1
    `$cmd = "tar -cz -C " . $_SERVER['DOCUMENT_ROOT'] . " download";` the `-C` option is equivalent to changing to the specified directory beforehand. – Sammitch Nov 18 '13 at 22:43

0 Answers0