skip to Main Content

I am currently working on an script that will easily import an backup in 2 clicks, but i can’t find any information on how to import and .gz file.
and .sql file works great but if im going to decompress it i can just aswell just import it using phpmyadmin.

when i currently try to import the .gz file i just get one massive error of unreadable characters.

How can i make it so i can import an .gz file

The point here is that it is an WEB based importer

$userName = "root";
$password = "";
$dbName = "test";

    $conn = @new mysqli($serverName,$userName,$password,$dbName);

    if ($conn->connect_errno) {
    echo "Failed to connect to MySQL: " . $con->connect_errno;
    echo "<br/>Error: " . $con->connect_error;}
    

    $fileName = "189_dump.sql.gz";
    $temp = "";
    $lines = file($fileName);

    foreach($lines as $line){
        if(substr($line, 0, 2) == '--' || $line == '')
        continue;

        $temp .= $line;

        if(substr(trim($line), -1, 1) == ';'){
            $conn->query($temp) or print('Error performing query '<strong>' . $temp . '': '. '<br /><br />');
            $temp = "";
        }
    }
    echo "imported db (or not)";


?> 

2

Answers


  1. Chosen as BEST ANSWER

    This is how i fixed it:

    $lines = gzfile($fileName);

    <?php
    
    class reader{
        
        public $dbName;
        public $fileName;
        
        public function import($dbName, $fileName){
            $serverName = "localhost";
            $userName = "root";
            $password = "";
    
            $conn = new mysqli($serverName,$userName,$password,$dbName);
    
            $temp = "";
            $lines = gzfile($fileName);
    
            foreach($lines as $key => $line ){
                if(substr($line, 0, 2) == '--' || $line == '') continue;
    
                $temp .= $line;
    
                if(substr(trim($line), -1, 1) == ';'){
                    $conn->query($temp) or print('Error performing query '<strong>' . $temp . '': '. '<br /><br />');
                    $temp = "";}
            
            }
            echo "<script>alert('Database Imported')</script>";
        }
    }
    $bestand = new reader();
    #               dbname  |  file name
    $bestand->import("test", "testdb.sql.gz");
    
    ?>
    
    

  2. If you have system() allowed you can:

    <?php
    system("gzip -dc < ".$file." | mysql -u $dbuser -p$dbpassword $dbname");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search