2013-04-29 4 views
0

Ceci est mon script:PHP/MySQL Database script de sauvegarde ne fonctionne pas correctement

<?php 

ini_set('display_errors',1); 
ini_set('display_startup_errors',1); 
error_reporting(-1); 

backup_tables('localhost','xxxxx','xxxxx','xxxxx'); 


/* backup the db OR just a table */ 
function backup_tables($host,$user,$pass,$name,$tables = '*') 
{ 

    $link = mysql_connect($host,$user,$pass); 
    mysql_select_db($name,$link); 

    //get all of the tables 
    if($tables == '*') 
    { 
     $tables = array(); 
     $result = mysql_query('SHOW TABLES'); 
     while($row = mysql_fetch_row($result)) 
     { 
      $tables[] = $row[0]; 
     } 
    } 
    else 
    { 
     $tables = is_array($tables) ? $tables : explode(',',$tables); 
    } 

    //cycle through 
    foreach($tables as $table) 
    { 
     $result = mysql_query('SELECT * FROM '.$table); 
     $num_fields = mysql_num_fields($result); 

     $return.= 'DROP TABLE '.$table.';'; 
     $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table)); 
     $return.= "\n\n".$row2[1].";\n\n"; 

     for ($i = 0; $i < $num_fields; $i++) 
     { 
      while($row = mysql_fetch_row($result)) 
      { 
       $return.= 'INSERT INTO '.$table.' VALUES('; 
       for($j=0; $j<$num_fields; $j++) 
       { 
        $row[$j] = addslashes($row[$j]); 
        $row[$j] = ereg_replace("\n","\\n",$row[$j]); 
        if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; } 
        if ($j<($num_fields-1)) { $return.= ','; } 
       } 
       $return.= ");\n"; 
      } 
     } 
     $return.="\n\n\n"; 
    } 

    $backup_file = $_SERVER['DOCUMENT_ROOT'] . '/scripts_and_crons/fr_internetlead-'.time().'.sql'; 
    //save file 
    $handle = fopen($backup_file,'w+'); 
    fwrite($handle,$return); 
    fclose($handle); 
} 

//define the receiver of the email 
$to = '[email protected]'; 
//define the subject of the email 
$subject = 'Test email with attachment'; 
//create a boundary string. It must be unique 
//so we use the MD5 algorithm to generate a random hash 
$random_hash = md5(date('r', time())); 
//define the headers we want passed. Note that they are separated with \r\n 
$headers = "From: [email protected]\r\nReply-To: [email protected]"; 
//add boundary string and mime type specification 
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\""; 
//read the atachment file contents into a string, 
//encode it with MIME base64, 
//and split it into smaller chunks 
$attachment = chunk_split(base64_encode(file_get_contents($backup_file))); 
//define the body of the message. 
ob_start(); //Turn on output buffering 
?> 
--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1" 
Content-Transfer-Encoding: 7bit 

<h2>Hello World!</h2> 
<p>This is something with <b>HTML</b> formatting.</p> 

--PHP-alt-<?php echo $random_hash; ?>-- 

--PHP-mixed-<?php echo $random_hash; ?> 
Content-Type: application/sql; name="<?php echo $backup_file; ?>" 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment 

<?php echo $attachment; ?> 
--PHP-mixed-<?php echo $random_hash; ?>-- 

<?php 
//copy current buffer contents into $message variable and delete current output buffer 
$message = ob_get_clean(); 
//send the email 
$mail_sent = @mail($to, $subject, $message, $headers); 
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed"; 

?> 

La seule information de débogage je reçois en php sont

Notice: Undefined variable: return in /var/www/vhosts/domain.com/httpdocs/scripts_and_crons/internetlead_backup.php on line 38

et

Deprecated: Function ereg_replace() is deprecated in /var/www/vhosts/domain.com/httpdocs/scripts_and_crons/internetlead_backup.php on line 50 x infini

La seule pièce jointe I recevoir dans l'email est ATT00001.

echo '<pre>'; 
echo $return; 
echo '</pre>'; 

Imprime les données correctes.

faire Même

echo '<pre>'; 
echo file_get_contents($backup_file); 
echo '</pre>'; 

Imprime les données correctes de sorte que le fichier est en cours d'écriture, mais pour une raison quelconque le script échoue à l'attacher à l'e-mail.

Vous ne pouvez pas voir ce qui pourrait se tromper, des idées?

Martin

MISE À JOUR

a trouvé la réponse dans une autre question SO - PHP mail() attachment problems

C'est la bonne façon de le faire.

+1

'Il doit être unique nous utilisons donc l'algorithme MD5 pour générer un hash' aléatoire qui a été – Bobby

+0

Aussi, 'mysql_ *' est obsolète et est remplacé par 'mysqli_ *' ou mieux encore 'PDO' – Bobby

+0

oui,' mysql_ * 'sont obsolètes, à part ça s'il vous plaît – martincarlin87

Répondre

0

Si votre version de php est supérieure à 5.2, vous devez remplacer ereg_replace avec preg_match et le changement (« parties comme («/

Questions connexes