Jump to content

save output file in a specified location automatically


cleary1981

Recommended Posts

Hi,

 

I have this script for backing up a database that when it is run the option comes up to save the file. Can anyone show me how I would change this script so that the script automatically saves the file in a specified folder?

 

<?php 
set_time_limit(0); 
$mysqldump_version="1.02"; 
  
  
$output_messages=array(); 
  
    $mysql_host= 'localhost'; 
    $mysql_database= '**';  
    $mysql_username= '**';  
    $mysql_password= '**';  
  
            _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password); 
  
  
  
            //ob_start("ob_gzhandler"); 
            header('Content-type: text/plain'); 
            header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".date('YmdHis').'.sql"'); 
            echo "/*mysqldump.php version $mysqldump_version */\n"; 
            _mysqldump($mysql_database); 
  
  
function _mysqldump($mysql_database) 
{ 
    $sql="show tables;"; 
    $result= mysql_query($sql); 
    if( $result) 
    { 
        while( $row= mysql_fetch_row($result)) 
        { 
            _mysqldump_table_structure($row[0]); 
  
            _mysqldump_table_data($row[0]); 
  
        } 
    } 
    else 
    { 
        echo "/* no tables in $mysql_database */\n"; 
    } 
    mysql_free_result($result); 
} 
  
function _mysqldump_table_structure($table) 
{ 
    echo "/* Table structure for table `$table` */\n"; 
    if( isset($_REQUEST['sql_drop_table'])) 
    { 
        echo "DROP TABLE IF EXISTS `$table`;\n\n"; 
    } 
    if( isset($_REQUEST['sql_create_table'])) 
    { 
  
        $sql="show create table `$table`; "; 
        $result=mysql_query($sql); 
        if( $result) 
        { 
            if($row= mysql_fetch_assoc($result)) 
            { 
                echo $row['Create Table'].";\n\n"; 
            } 
        } 
        mysql_free_result($result); 
    } 
} 
  
function _mysqldump_table_data($table) 
{ 
  
    $sql="select * from `$table`;"; 
    $result=mysql_query($sql); 
    if( $result) 
    { 
        $num_rows= mysql_num_rows($result); 
        $num_fields= mysql_num_fields($result); 
  
        if( $num_rows > 0) 
        { 
            echo "/* dumping data for table `$table` */\n"; 
  
            $field_type=array(); 
            $i=0; 
            while( $i < $num_fields) 
            { 
                $meta= mysql_fetch_field($result, $i); 
                array_push($field_type, $meta->type); 
                $i++; 
            } 
  
            //print_r( $field_type); 
            echo "insert into `$table` values\n"; 
            $index=0; 
            while( $row= mysql_fetch_row($result)) 
            { 
                echo "("; 
                for( $i=0; $i < $num_fields; $i++) 
                { 
                    if( is_null( $row[$i])) 
                        echo "null"; 
                    else 
                    { 
                        switch( $field_type[$i]) 
                        { 
                            case 'int': 
                                echo $row[$i]; 
                                break; 
                            case 'string': 
                            case 'blob' : 
                            default: 
                                echo "'".mysql_real_escape_string($row[$i])."'"; 
  
                        } 
                    } 
                    if( $i < $num_fields-1) 
                        echo ","; 
                } 
                echo ")"; 
  
                if( $index < $num_rows-1) 
                    echo ","; 
                else 
                    echo ";"; 
                echo "\n"; 
  
                $index++; 
            } 
        } 
    } 
    mysql_free_result($result); 
    echo "\n"; 
} 
  
function _mysql_test($mysql_host,$mysql_database, $mysql_username, $mysql_password) 
{ 
    global $output_messages; 
    $link = mysql_connect($mysql_host, $mysql_username, $mysql_password); 
    if (!$link) 
    { 
       array_push($output_messages, 'Could not connect: ' . mysql_error()); 
    } 
    else 
    { 
        array_push ($output_messages,"Connected with MySQL server:$mysql_username@$mysql_host successfully"); 
  
        $db_selected = mysql_select_db($mysql_database, $link); 
        if (!$db_selected) 
        { 
            array_push ($output_messages,'Can\'t use $mysql_database : ' . mysql_error()); 
        } 
        else 
            array_push ($output_messages,"Connected with MySQL database:$mysql_database successfully"); 
    } 
  
} 
?> 

I don't really understand that.

 

I know I have to change this part of my code

header('Content-type: text/plain'); 
            header('Content-Disposition: attachment; filename="'.$mysql_host."_".$mysql_database."_".date('YmdHis').'.sql"'); 
            echo "/*mysqldump.php version $mysqldump_version */\n"; 
            _mysqldump($mysql_database); 
  

 

Can anyone show me how?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.