darubillah Posted March 19, 2011 Share Posted March 19, 2011 hi, I am trying to restore a mysql db from a remote location below is the function it is working fine when i am passing local location, but gives error on passing remote location function restore($path) { $f = fopen('restore.sql' , 'w+'); if(!$f) { echo "Error While Restoring Database"; return; } $zip = new ZipArchive(); if ($zip->open($path) === TRUE) { #Get the backup content $sql = $zip->getFromName('adobe4u_new-sqldump.sql'); #Close the Zip File $zip->close(); #Prepare the sql file fwrite($f , $sql); fclose($f); #Now restore from the .sql file //$command = "mysql --user=root --password=password --database=adobe4u < restore.sql"; //exec($command); // Name of the file $filename = 'restore.sql'; // 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 mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />'); // Reset temp variable to empty $templine = ''; } } // Restoring END #Delete temporary files without any warning @unlink('restore.sql'); echo '<div class="success">Successfully Updated!</div>'; } else { echo '<div class="error">Some thing went wrong! Updation Failed</div>'; } } Please can anyone help me in it, so That if i pass a remote location of database it will extract it locally Quote Link to comment https://forums.phpfreaks.com/topic/231130-restore-db-from-a-remote-website/ Share on other sites More sharing options...
johnny86 Posted March 19, 2011 Share Posted March 19, 2011 Have you checked that you have allow_url_fopen enabled? http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen Quote Link to comment https://forums.phpfreaks.com/topic/231130-restore-db-from-a-remote-website/#findComment-1189683 Share on other sites More sharing options...
darubillah Posted March 20, 2011 Author Share Posted March 20, 2011 i am on shared hosting is their any way to open it through PHP or can you help me in coding it using cURL? Quote Link to comment https://forums.phpfreaks.com/topic/231130-restore-db-from-a-remote-website/#findComment-1189929 Share on other sites More sharing options...
darubillah Posted March 20, 2011 Author Share Posted March 20, 2011 Problem solved what we have to do is to download the file from remote server to local <?php // maximum execution time in seconds set_time_limit (24 * 60 * 60); //File Location to restore the downloaded DB $filename = 'backup/backup.zip'; //Checking if database already exists then delete it first if (file_exists($filename)) { @unlink($filename); } // folder to save downloaded files to. must end with slash $destination_folder = 'backup/'; //File URL to download zip database $url = "http://example.com/backup.zip"; $newfname = $destination_folder . basename($url); $file = fopen ($url, "rb"); if ($file) { $newf = fopen ($newfname, "wb"); if ($newf) while(!feof($file)) { fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 ); } } if ($file) { fclose($file); } if ($newf) { fclose($newf); } //Call above restore function to restore database restore($filename) ?> Quote Link to comment https://forums.phpfreaks.com/topic/231130-restore-db-from-a-remote-website/#findComment-1190008 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.