Well, I went through a lot of articles with respect to the above mentioned topic, but I was unable to find one which I really wanted. Most of the articles informs you how to backup the data and send a mail to the user from where they can download the archived file.
My requirement was to download the archived file at the click of the button without sending a mail to the end user. The user should be able to download the file from the browser interface.
require('../config/config.php');
if($_GET["filename"]!="")
{
$filename=$_GET["filename"];
$handle = @fopen($filename, 'r');
if (!$handle) {
exit(0);
}
$time= date('Ymd');
header("Content-type: application/force-download");
header("Content-Disposition: attachment; filename=psp-".$time.".zip");
while(!feof($handle) and (connection_status()==0)) {
print(fread($handle, 1024*8));
flush();
}
fclose($handle);
}
else
{
system("mysqldump -u $dbusername --password=$dbpasswd psp > /tmp/psp.sql");
system("cd /tmp;zip -r psp psp.sql >/dev/null");
header( "Location: http://$domainname/custom/mysqlbackup.php?filename=/tmp/psp.zip"););
}
`config.php` includes all the configuration parameters for connecting to the database. Since we are using a single file for posting, we check in the `url` whether the file name has been passed or not. In case the file name has been passed, we add headers to the content-type for download. You need change the download filename. As per my requirements I have used `psp` followed by the date when the download takes place.
I have used `mysqldump` for backing up the database. This is not an efficient way of taking a backup since the `username` and the `password` are sent across the network in plain text. Any user using a network sniffing tool can get hold of the `username` and the `password`. I preferred to use in this way as my script is not exposed to the internet. The last line indicates the reloading of the same page with the archived file name.
Leave a Reply