0

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..

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by kumiyare
0

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.

Seems to me that you answered your own question!
Supply the proper file path to the script.

0

Seems to me that you answered your own question!
Supply the proper file path to the script.

JRM, Thanks for your quick reply

What I need is: the users will browse the file from their normal computers not the server, if so how can i supply the proper path?.

If you look deeply in the source code you can see my problem, plz test it first and then if there are errors fix them, that would be great help for me.

Thanks again

0

You would need to use the files temp directory to get its data. You cannot read it from their computer. $_FILES will return the name of the file as it is on their system which doesn't matter once its uploaded.

Just replace $_FILES with $_FILES. That should work.

0

kkeith29,

Thanks indeed, it really worked for, great help, you are smart thanks again and again.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.