Marvin De Leon Marvin De Leon - 7 months ago 41
MySQL Question

restore database in php

I'm trying to restore my database using this code and it doesn't work

$server_name = "localhost";
$username = "root";
$password = "admin";
$database_name = "uvatravelclone";
$file = basename($_POST["file_open"]);
//$cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name} $file";
$cmd = "mysql -e {$file}";
if (exec($cmd) == true){
echo "succes";
echo " error";

i need to get the file name or file path of the data because i'm using

<input type=file>

to open the file.


When you use <input type="file" name="file_open">, the information about the uploaded file is put in $_FILE, not $_POST. The file contents are put into a temporary file, and $_FILE['file_open']['tmp_name'] contains the pathname to this temporary file. You can then redirect this to the mysql command, using < filename for shell input redirection.

$server_name   = "localhost";
$username      = "root";
$password      = "admin";
$database_name = "uvatravelclone";
$file = $_FILE['file_open']['tmp_name']
$cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name} < $file";
if (exec($cmd)){
   echo "success";
   echo " error";

Note that the return value of exec() is the last line of output of the command, not a true/false value that indicates success.