PHP can be a button to export MySQL backup files, and compressed storage, although phpMyAdmin has this feature, but if you develop your own website or write a CMS for others, you should not ask others to use your program to use the other phpMyAdmin backup MySQL, This should be a function of your CMS, then php How to implement a key backup MySQL data, here refers to the MySQL content is exported to SQL files, and then compressed SQL, two methods are described as follows:
The first type:
<?PHP$username= "Root";//your MySQL user name$password= "";//Password$hostname= "localhost";//MySQL server address$dbname= "Cars";//Database name$dumpfname=$dbname. "_" .Date("Y-m-d_h-i-s"). ". sql;$command= "C:\\xampp\\mysql\\bin\\mysqldump--add-drop-table--host=$hostname--user=$username";if($password)$command. = "--password=".$password." ";$command.=$dbname;$command. = ">".$dumpfname;system($command);//compress into a zip file$zipfname=$dbname. "_" .Date("Y-m-d_h-i-s"). ". Zip;$zip=Newziparchive ();if($zip->open ($zipfname, Ziparchive::CREATE)) { $zip->addfile ($dumpfname,$dumpfname); $zip-close ();}if(file_exists($zipfname)) { Header(' Content-description:file Transfer '); Header(' Content-type:application/octet-stream '); Header(' Content-disposition:attachment; Filename= '.basename($zipfname)); Flush(); ReadFile($zipfname); Exit;}?>
The above code can be saved as a PHP file, such as mysqlbak.php, note that this file must have write permissions. For ease of use, you can give this file a link in the background, you need to export MySQL, you just click on the backup export operation.
The second method: Do not require write permission, but do not compress the SQL file, the code is as follows:
<?PHPOb_start();$username= "Root";$password= "";$hostname= "localhost";$dbname= "Test";$command= "C:\\xampp\\mysql\\bin\\mysqldump--add-drop-table--host=$hostname--user=$username";if($password)$command. = "--password=".$password." ";$command.=$dbname;system($command);$dump=ob_get_contents();Ob_end_clean();Header(' Content-description:file Transfer ');Header(' Content-type:application/octet-stream ');Header(' Content-disposition:attachment; Filename= '.basename($dbname. "_" .Date("Y-m-d_h-i-s"). ". sql));Flush();Echo $dump;Exit();?>
Two methods you can choose one, the same can be saved to the second method of mysqlbak.php file, in the background to give a link, with convenience.
PHP exports MySQL database and compresses