Hi every body!,
I am new to PHP and I'm developing a basic PHP CMS, now specially I'm developing the database backup & restore section of the CMS, so I've experienced one problem.
I want that CMS users able to upload their database back ups to restore, they will use a simple form and browse their sql file then submit, my problem is if the browsed file reside in the same directory as script it works fine, but when the file is outside of the script directory it is not working.
Any help is appreciated <Below is the script file source code>
<? if(isset($_POST['submit'])){
// Name of the file
$filename = $_FILES["file"]["name"];
// MySQL host
$mysql_host = 'localhost';
// MySQL username
$mysql_username = 'root';
// MySQL password
$mysql_password = ' ';
// Database name
$mysql_database = ' ';
//////////////////////////////////////////////////////////////////////////////////////////////
// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());
/* query all tables */
$sql = "SHOW TABLES FROM $mysql_database";
if($result = mysql_query($sql)){
/* add table name to array */
while($row = mysql_fetch_row($result)){
$found_tables[]=$row[0];
}
}
else{
die("Error, could not list tables. MySQL Error: " . mysql_error());
}
/* loop through and drop each table */
foreach($found_tables as $table_name){
$sql = "DROP TABLE $mysql_database.$table_name";
if(!$result = mysql_query($sql)){
echo "Error deleting $table_name. MySQL Error: " . mysql_error() . "";
}
}
// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it's a comment
if (substr($line, 0, 2) == '--' || $line == '')
continue;
// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it's the end of the query
if (substr(trim($line), -1, 1) == ';')
{
// Perform the query
$restore = mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
// Reset temp variable to empty
$templine = '';
}
}
echo "The database has been restored";
}else {
?>
<form id="myform" name="myform" method="post" action="restores.php" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<br>
<input type="submit" name="submit" id="button" value="Submit">
<input type="reset" name="reset" id="reset" value="Reset">
</form>
<? }?>
Thanks if you help me..