PHP system command

Hi,
I have this question, maybe a little stupid.
I want to run system command to make backup of databases. I force the browser to offer download box, but my problem is that system command will save the backup copy in the folder from where it is executed or other specified path.
Would appreciate any help with this, if i can just force download box and not save it some where else, or maybe keep it as an option.
Here is the code:

$backupfile = $dbname."-".date("Ymd-His").".".$type; // .gz , .zip , .sql
if($type == 'sql'){
	$command = "mysqldump -h $hostname -u$username -p$password $dbname > $backupfile";
}
else
{
	$command = "mysqldump -h $hostname -u$username -p$password $dbname | gzip > $backupfile";
}
system($command);

header ("Content-Type: application/download");
header ("Content-Disposition: attachment; filename=$backupfile");
header("Content-Length: " . filesize("$backupfile"));
$fp = fopen("$backupfile", "r");
fpassthru($fp);
readfile("$backupfile");
fclose($fp);
exit();

Also would not mind if some one want to enlighten the header thing with fpassthru.

Regards

I’m not sure if there is any reasonable way to pipe the output of a system command directly to the php output buffer. But after sending the file to the browser just delete it from the server. No need to keep it around if you don’t want to.

$fp = fopen($fileName)
fpassthru($fp)
fclose($fp);
unlink($fileName);

Maybe i did not clearly ask the question, let me rephrase it.
Lets say my script is in /var/www/projects/ and i run the script to make backup, at the mean time when it ask for saving the file in other location through dialog box, it also backup a copy in /var/www/projects/ folder. Which i want to avoid to not save another copy, just the dialog box one.

  1. Get rid of the > $backupfile in the mysqldump command
  2. Use [fphp]exec[/fphp] instead of [fphp]system[/fphp] and use the second parameter to capture the output
  3. The output of exec() is an array, so [fphp]implode[/fphp] that with "
    " to get a string
  4. The filesize for the headers is the [fphp]strlen[/fphp] of the string obtained in (3)
  5. Send the string to the browser (echo it)

Voila :slight_smile:

Good idea though. A temporary solution, but worth to use.
Thanks

I’m not sure I understand the problem. You can put whatever filename you want in:
header (“Content-Disposition: attachment; filename=$backupfile”);

And then use a completely different name/path when you generate the file on the server. Perhaps a random name in the tmp directory. Or perhaps I’m completely missing the question.