Hey people,
How can i make this work,
It sends the email, makes the file, deletes the file
But when it sends the email, it doesnt bloooming attach the file
🙁
If you can fix this code GREAT!!!!!!!!!!!!
<?php
error_reporting(E_ERROR);
$email = "w_pearsall@yahoo.co.uk"; // Who to send the attached Database backup to.
$filename = "E:/Servers/Root/*****/backup.sql"; //Name of the file u want to send as an attachment.
$dbname = $_REQUEST[dbname];
$dbuname = $_REQUEST[dbuname];
$dbpass = $_REQUEST[dbpass];
$dbhost = "localhost";
if($_REQUEST['my_pass'] == "password"){
$logged_in = "1";
} else {
die("<html>\nOI!, TELL ME THE PROPER \"SECRET WORD\"\n</html>");
$logged_in = "0";
exit;
}
if ($logged_in == "1") {
$content = "";
switch($op) {
case "backup":
@set_time_limit(800);
echo "<HTML>\n<head>\n<title>Backing Up $dbname</title>\n";
echo "</head>\n<body>\n";
echo "Starting Backup Process of \"$dbname\" At ".date("H:i:s")."\n";
echo "<script language=\"JavaScript\"> window.status = \"Starting Backup Process of $dbname At: ".date("H:i:s")."\"</script>";
echo "<br>\n";
echo "Collecting Database Data of \"$dbname\" At ".date("H:i:s")."\n";
echo "<script language=\"JavaScript\"> window.status = \"Collecting Database Data of \\\"$dbname\\\" At: ".date("H:i:s")."\"</script>";
echo "<br>\n";
$crlf="\n";
// English Text
$strNoTablesFound = "No tables found in database.";
$strHost = "Host";
$strDatabase = "Database ";
$strTableStructure = "Table structure for table";
$strDumpingData = "Dumping data for table";
$strError = "Error";
$strSQLQuery = "SQL-query";
$strMySQLSaid = "MySQL said: ";
$strBack = "Back";
$strFileName = "Save Database";
$strName = "Database saved";
$strDone = "On";
$strat = "at";
$strby = "by";
$date_jour = date ("m-d-Y");
// doing some DOS-CRLF magic...
$client = getenv("HTTP_USER_AGENT");
if(ereg('[^(]*\((.*)\)[^)]*',$client,$regs))
{
$os = $regs[1];
// this looks better under WinX
if (eregi("Win",$os))
$crlf="\r\n \n";
}
function my_handler($sql_insert)
{
global $crlf;
$content .= "$sql_insert;$crlf";
}
// Get the content of $table as a series of INSERT statements.
// After every row, a custom callback function $handler gets called.
// $handler must accept one parameter ($sql_insert);
function get_table_content($db, $table, $handler)
{
$result = mysql_db_query($db, "SELECT * FROM $table") or mysql_die();
$i = 0;
while($row = mysql_fetch_row($result))
{
// set_time_limit(60); // HaRa
$table_list = "(";
for($j=0; $j<mysql_num_fields($result);$j++)
$table_list .= mysql_field_name($result,$j).", ";
$table_list = substr($table_list,0,-2);
$table_list .= ")";
if(isset($GLOBALS["showcolumns"]))
$schema_insert = "INSERT INTO $table $table_list VALUES (";
else
$schema_insert = "INSERT INTO $table VALUES (";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= " NULL,";
elseif($row[$j] != "")
$schema_insert .= " '".addslashes($row[$j])."',";
else
$schema_insert .= " '',";
}
$schema_insert = ereg_replace(",$", "", $schema_insert);
$schema_insert .= ")";
$handler(trim($schema_insert));
$i++;
}
return (true);
}
// Return $table's CREATE definition
// Returns a string containing the CREATE statement on success
function get_table_def($db, $table, $crlf)
{
$schema_create = "";
//$schema_create .= "DROP TABLE IF EXISTS $table;$crlf";
$schema_create .= "CREATE TABLE $table ($crlf";
$result = mysql_db_query($db, "SHOW FIELDS FROM $table") or mysql_die();
while($row = mysql_fetch_array($result))
{
$schema_create .= " $row[Field] $row[Type]";
if(isset($row["Default"]) && (!empty($row["Default"]) || $row["Default"] == "0"))
$schema_create .= " DEFAULT '$row[Default]'";
if($row["Null"] != "YES")
$schema_create .= " NOT NULL";
if($row["Extra"] != "")
$schema_create .= " $row[Extra]";
$schema_create .= ",$crlf";
}
$schema_create = ereg_replace(",".$crlf."$", "", $schema_create);
$result = mysql_db_query($db, "SHOW KEYS FROM $table") or mysql_die();
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'];
}
while(list($x, $columns) = @each($index))
{
$schema_create .= ",$crlf";
if($x == "PRIMARY")
$schema_create .= " PRIMARY KEY (" . implode($columns, ", ") . ")";
elseif (substr($x,0,6) == "UNIQUE")
$schema_create .= " UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";
else
$schema_create .= " KEY $x (" . implode($columns, ", ") . ")";
}
$schema_create .= "$crlf)";
return (stripslashes($schema_create));
}
function mysql_die($error = "")
{
$content .= "<b> $strError </b><p>";
if(isset($sql_query) && !empty($sql_query))
{
$content .= "$strSQLQuery: <pre>$sql_query</pre><p>";
}
if(empty($error))
$content .= $strMySQLSaid.mysql_error();
else
$content .= $strMySQLSaid.$error;
$content .= "<br><a href=\"javascript:history.go(-1)\">$strBack</a>";
exit;
}
global $dbhost, $dbuname, $dbpass, $dbname;
mysql_pconnect($dbhost, $dbuname, $dbpass);
@mysql_select_db("$dbname") or die ("Unable to select database");
$tables = mysql_list_tables($dbname);
$num_tables = @mysql_numrows($tables);
if($num_tables == 0)
{
$content .= $strNoTablesFound;
}
else
{
$i = 0;
$heure_jour = date ("H:i");
$content .= "# ========================================================$crlf";
$content .= "#$crlf";
$content .= "# $strName : $dbname$crlf";
$content .= "# $strDone $date_jour $strat $heure_jour $strby Database_Backuper.exe !$crlf";
$content .= "#$crlf";
$content .= "# ========================================================$crlf";
$content .= "$crlf";
while($i < $num_tables)
{
$table = mysql_tablename($tables, $i);
$content .= $crlf;
$content .= "# --------------------------------------------------------$crlf";
$content .= "#$crlf";
$content .= "# $strTableStructure '$table'$crlf";
$content .= "#$crlf";
$content .= $crlf;
$content .= get_table_def($dbname, $table, $crlf).";$crlf$crlf";
$content .= "#$crlf";
$content .= "# $strDumpingData '$table'$crlf";
$content .= "#$crlf";
$content .= $crlf;
get_table_content($dbname, $table, "my_handler");
$i++;
}
}
echo "Writing Database Data of \"$dbname\" To File At ".date("H:i:s")."\n";
echo "<script language=\"JavaScript\"> window.status = \"Writing Database Data of \\\"$dbname\\\" To File At: ".date("H:i:s")."\"</script>";
echo "<br>\n";
$file = fopen($filename, "w");
fwrite($file, $content);
fclose($file);
$userfile_type = "Text/HTML";
//MIME code
$boundary = "b" . md5(uniqid(time()));
$mime = "Content-type: multipart/mixed;";
$mime .= "boundary = $boundary\r\n\r\n";
$mime .= "This is a mime encoded message.\r\n\r\n";
//First the regular message
$mime_message .= "--$boundary\r\n";
$mime .= "Content-type: $userfile_type\r\n";
$mime .= "Content-Transfer-Encoding: base64\r\n\r\n";
//$mime .= "\r\n\r\n" . chunk_split(base64_encode($message)) . "\r\n";
//Now the attachment
if(!($fp = @fopen($filename, "r"))){
$error = "Can't open file";
echo $error;
exit;
}
$attach = fread($fp, filesize($filename));
$attach = chunk_split(base64_encode($attach));
$mime .= "--$boundary\r\n";
$mime .= "File Name: $filename\r\n";
$mime .= "Content-type: $userfile_type\r\n";
$mime .= "Content-Transfer-Encoding: base64\r\n\r\n";
$mime .= "\r\n\r\n$attach\n";
echo "Sending Database \"$dbname\" To EMail Address \"$email\" At ".date("H:i:s")."\n";
echo "<script language=\"JavaScript\"> window.status = \"Sending Database \\\"$dbname\\\" To EMail Address \\\"$email\\\" At ".date("H:i:s")."\"</script>";
mail($email, "Database Backup of ".$dbname." At ".date("H:i:s"), "Your database should be attached!", "From: Auto_EMail@".getenv("SERVER_NAME")."\n", $mime);
echo "<br>\n";
echo "Deleting \"$filename\" At ".date("H:i:s")."";
echo "<script language=\"JavaScript\"> window.status = \"Deleting \\\"$filename\\\" At ".date("H:i:s")."\" </script>";
echo "<br>\n";
unlink($filename);
echo "<B>Back up process Complete At ".date("H:i:s")."</B>";
echo "<script language=\"JavaScript\"> window.status = \"Backup Process Complete At ".date("H:i:s")."\" </script>";
break;
}
}
?>
Now, Ignore any errors in my j*vascript window.status = "" stuff,
but i think that this board hates it 😉
I Get no errors (even with error_reporting(E_ALL)😉
THANKS!
See ya 🙂