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

Dani AI

Generated

The problem was not PHP itself but the path you were trying to feed to file(). Browsers do not expose a readable path on the user’s machine to your server. The uploaded file is placed on the server in a temporary file. was correct that a correct path is needed, but the path has to be the server temp path. pointed to the temp file idea (there was a small typo in that post), and the later thank-you from confirms that switching to the uploaded temp file fixed it.

Use the uploaded temp name ($_FILES['file']['tmp_name']) or move the uploaded file to a known server path before reading it. Always check $_FILES['file']['error'] first, verify is_uploaded_file() and use move_uploaded_file() for safety. Example pattern:

if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
  $tmp = $_FILES['file']['tmp_name'];          // server temp file
  $contents = file_get_contents($tmp);         // read SQL from server temp
  // or move to a permanent location:
  // move_uploaded_file($tmp, '/path/to/uploads/' . basename($_FILES['file']['name']));
}

Other practical notes: increase upload_max_filesize / post_max_size / max_execution_time if imports are large; validate the file (extension, size, optionally content); store uploads outside webroot and with tight permissions; and prefer importing very large SQL dumps with the MySQL client on the server rather than iterating queries in PHP. Also, avoid the old mysql_* extension on modern PHP—use mysqli or PDO instead. See the PHP file-upload guide and the move_uploaded_file() docs for details: PHP file upload (POST) docs and move_uploaded_file(). For compatibility notes on mysql_connect(), see the PHP manual entry for that function.

Recommended Answers

All 4 Replies

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.

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

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.

kkeith29,

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.