Hallo, ich habe folgendes Script welches mir momentan per Cronjob wöchentlich eine Tabelle als .sql per Email sendet.
Leider ist das Format .sql etwas ungünstig für die weitere Verarbeitung der Tabelle z.B. in Excel. Nun möchte ich das Script umprogrammieren, sodass es mir die Tabelle im .csv-Format sendet. Wie mache ich das?
Hier das Script:
	
Vielen Dank für Eure Hilfe
Headhunter007
					Leider ist das Format .sql etwas ungünstig für die weitere Verarbeitung der Tabelle z.B. in Excel. Nun möchte ich das Script umprogrammieren, sodass es mir die Tabelle im .csv-Format sendet. Wie mache ich das?
Hier das Script:
PHP Code:
	
	
<?php 
@set_time_limit(0); 
//Verbindung zur Datenbank 
$verbindung = mysql_connect("mysql.de","dbname","passwort") 
or die("Username/Passwort falsch"); 
mysql_select_db("dbname"); 
// MySQL Tabellennamen 
$tbname = array(); 
$tbname[]= "proben";  
// 0: Normale Datei 
// 1: GZip-Datei 
$compression = 0; 
//Struktur mitspeichern? 
//0: Nein 
//1: Ja 
$struktur = 1; 
//Falls Gzip nicht vorhanden, kein Gzip 
if(!extension_loaded("zlib")) 
   $compression = 0; 
// Pfad zur aktuellen Datei 
$path = ereg_replace ("\\\\","/",__FILE__); 
$path = dirname ($path); 
$path = trim($path); 
// Pfad zum Backup 
$path .= "/backup/"; 
//Speicherart 
//0: Auf dem Server speichern 
//1: Per Email versenden 
//2: Zum Download anbieten 
$send = 1; 
//Email-Adresse für Backup 
$email = "kontakt@email.de"; 
//Dateityp 
if ($compression==1) $filetype = "sql.gz"; 
else $filetype = "sql"; 
//Dateieigenschaften 
$cur_time=date("d.m.Y H:i"); 
$cur_date=date("Y-m-d"); 
//Pfade zu den neuen Backup-Dateien (fur den Mailversand) 
//__Nicht verändern___ 
$backup_pfad = array(); 
//Erstelle Eintäge von Tabelle 
function get_content($table) { 
    $content=""; 
    $result = mysql_query("SELECT * FROM $table") 
OR $content = "# $table nicht vorhanden\n #".mysql_error(); 
    while($row = @mysql_fetch_row($result)) { 
        $insert = "INSERT INTO $table VALUES ("; 
        for($j=0; $j<mysql_num_fields($result);$j++) { 
            if(!isset($row[$j])) $insert .= "NULL,"; 
            else if($row[$j] != "") $insert .= "'".addslashes($row[$j])."',"; 
            else $insert .= "'',"; 
        } 
        $insert = ereg_replace(",$","",$insert); 
        $insert .= ");\n"; 
        $content .= $insert; 
    } 
    return $content; 
} 
//Erstelle Struktur von Datenbank 
function get_def($table) { 
    $def = ""; 
    $def .= "CREATE TABLE $table (\n"; 
    $result = mysql_query("SHOW FIELDS FROM $table"); 
    while($row = @mysql_fetch_array($result)) { 
        $def .= "    `$row[Field]` $row[Type]"; 
        if ($row["Default"] != "") $def .= " DEFAULT '$row[Default]'"; 
        if ($row["Null"] != "YES") $def .= " NOT NULL"; 
        if ($row[Extra] != "") $def .= " $row[Extra]"; 
        $def .= ",\n"; 
    } 
    $def = ereg_replace(",\n$","", $def); 
    $result = mysql_query("SHOW KEYS FROM $table"); 
    while($row = @mysql_fetch_array($result)) { 
          $kname=$row[Key_name]; 
          if(($kname != "PRIMARY") && ($row[Non_unique] == 0)) $kname="UNIQUE|$kname"; 
          if(!isset($index[$kname])) $index[$kname] = array(); 
          $index[$kname][] = $row[Column_name]; 
    } 
    if(is_array($index)) { 
    while(list($x, $columns) = each($index)) { 
          $def .= ",\n"; 
          if($x == "PRIMARY") $def .= "  PRIMARY KEY (" . implode($columns, ", ") . ")"; 
          else if (substr($x,0,6) == "UNIQUE") 
          $def .= "  UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")"; 
          else $def .= "  KEY $x (" . implode($columns, ", ") . ")"; 
    } 
    } 
    $def .= "\n);"; 
    return (stripslashes($def)); 
} 
//Funktion um Backup auf dem Server zu speichern 
function write_backup($data) 
   { 
   global $path,$cur_date,$compression,$filetype; 
   $pfad = $path.$cur_date."_backup.".$filetype; 
   if ($compression==1) 
      { 
      $fp = gzopen($pfad,"w9"); 
      gzwrite ($fp,$data); 
      gzclose ($fp); 
      } 
   else 
      { 
      $fp = fopen ($pfad,"w"); 
      fwrite ($fp,$data); 
      fclose ($fp); 
      } 
   echo "<h3>Backup ist fertig</h3>"; 
   } 
//Backup per Email verschicken 
function mail_att($to, $from, $subject, $message, $data) 
   { 
   // $to xxx.de 
   // $from [email]kontakt@email.de[/email] ("email@domain.de" oder "Name <email@domain.de>") 
   // $subject Datenbankdaten für Produktproben profemina care  
   // $message Aktuell als Attachment die Daten - Proben 
   global $compression,$filetype; 
   if($compression == 1) 
      { 
      $data = @gzencode($data); 
      $app = "application/x-gzip"; 
      } 
   else 
       $app = "text/plain"; 
   $name = date("d-m-Y")."_backup.".$filetype; 
   $mime_boundary = "-----=" . md5(uniqid(rand(), 1)); 
   $header = "From: ".$from."\r\n"; 
   $header.= "MIME-Version: 1.0\r\n"; 
   $header.= "Content-Type: multipart/mixed;\r\n"; 
   $header.= " boundary=\"".$mime_boundary."\"\r\n"; 
   $content = "This is a multi-part message in MIME format.\r\n\r\n"; 
   $content.= "--".$mime_boundary."\r\n"; 
   $content.= "Content-Type: text/plain charset=\"iso-8859-1\"\r\n"; 
   $content.= "Content-Transfer-Encoding: 7bit\r\n\r\n"; 
   $content.= $message."\r\n"; 
   $data = chunk_split(base64_encode($data)); 
   $len = strlen($data); 
   $content.= "--".$mime_boundary."\r\n"; 
   $content.= "Content-Disposition: attachment;\r\n"; 
   $content.= "\tfilename=\"$name\";\r\n"; 
   $content.= "Content-Length: .$len;\r\n"; 
   $content.= "Content-Type: ".$app."; name=\"".$name."\"\r\n"; 
   $content.= "Content-Transfer-Encoding: base64\r\n\r\n"; 
   $content.= $data."\r\n"; 
   if(mail($to, $subject, $content, $header)) 
      echo "<h3>Backup wurde per Email versendet</h3>"; 
   else 
      echo "<h3>Backup konnte _nicht_ gesendet werden</h3>"; 
   } 
//Backup als Download anbieten 
function download($output) 
   { 
   global $compression,$filetype; 
   if($compression == 1) 
      { 
      $ausgabe = @gzencode($output); 
      if($ausgabe == false) 
         { 
         $ausgabe = $output; 
         $compression = 0; 
         } 
      } 
   else 
      $ausgabe = $output; 
   $filename = date("d-m-y")."_backup"; 
   if($compression == 1) 
      $app = "application/x-gzip"; 
   else 
      $app = "application/octetstream"; 
   ob_start(); 
   header("Cache-control: private"); 
   header("Content-disposition: filename={$filename}.{$filetype}"); 
   header("Content-type: $app"); 
   header("Pragma: no-cache"); 
   header("Expires: 0"); 
   echo $ausgabe; 
   ob_end_flush(); 
   } 
//Backup erstellen 
$tabellen = implode("\n#",$tbname); 
$output = "############################ 
# $cur_time: $datum 
# Tabellen: $tabellen 
# Datenbankbackup 
############################ 
"; 
if($struktur == 1) 
   { 
   foreach($tbname AS $table) 
      { 
      $output .= "\n# ----------------------------------------------------------\n#\n"; 
      $output .= "# Structur for Table '$table'\n#\n"; 
      $output .= get_def($table); 
      $output .= "\n\n"; 
      } 
   } 
$output .= "\n\n\n\n\n"; 
foreach($tbname AS $table) 
   { 
   $output .= "\n# ----------------------------------------------------------\n#\n"; 
   $output .= "# Data for Table '$table'\n#\n"; 
   $output .= get_content($table); 
   $output .= "\n\n"; 
   } 
//Backup speichern&versenden 
$text="Proben-Backup vom: ".date("d.m.Y H:i")."\n\n\n -- [url]
[url]www.webdesign.de[/url][/url] -- Wir bringen auch Ihre Firma ins Internet!";
$from = "kontakt@email.de"; 
switch($send) 
   { 
   case 0: write_backup($output); break; 
   case 1: mail_att($email, $from, "Proben-Backup vom: ".date("Y-m-d"), $text, $output); 
break; 
   case 2: download($output); break; 
   default: download($output); break; 
   } 
?>
Headhunter007
 
          
Comment