mysqldump
needs to be dumped into a file, otherwise it just returns the results as text.
Usually this is done by redirecting into a file:
mysqldump --... > dump.sql
Pergunta
I'm trying to create a page with a button when clicked will download the database named sampledb
.
Here's my backup.php
<?php
ob_start();
$username = "root";
$password = "";
$hostname = "localhost";
$dbname = "sampledb";
$command = "C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin\\mysqldump --add-drop-table --host=" . $hostname . "--user=". $username ." --password=". $password." ".$dbname;
system($command);
$dump = ob_get_contents();
ob_end_clean();
// send dump file to the output
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=sqlfile.sql');
readfile('sqlfile.sql');
flush();
echo $dump;
exit();
?>
The export was successful. However, when I checked the downloaded file (sqlfile.sql) in the downloads folder, its file size is only 2kB. I tried exporting the sampledb
using phpMyAdmin and found out the actual file size is 9kB.
Moreover, when I tried importing the sqlfile.sql
through phpMyAdmin, I got an error message. It says Warning: readfile(sqlfile.sql)
.
Something is wrong with my export code. Any suggestions?
UPDATE 20131205
I tried appending > sqlfile.sql
to this part of my code.
$command = "C:\\wamp\\bin\\mysql\\mysql5.5.24\\bin\\mysqldump. --add-drop-table --host=" . $hostname . "--user=". $username ." --password=". $password." ".$dbname;
File size is still incorrect. I cannot upload the downloaded file to phpMyAdmin.
END UPDATE
Solução
mysqldump
needs to be dumped into a file, otherwise it just returns the results as text.
Usually this is done by redirecting into a file:
mysqldump --... > dump.sql