PDA

View Full Version : خواندن دیتابیس و ریختن تو فایل



ruhy
شنبه 11 مرداد 1393, 22:51 عصر
سلام
یک فانکشن داریم به شکل زیر که اطلاعات دیتابیس رو میخونه و میزاره برای دانلود . حالا اگر بخاهیم اطلاعات رو تو یک فایل در مسیر جاری قرار بده باید چکار کنیم ؟



<?php
function backup_db($host, $user, $pass, $name, $tables = '*') {
date_default_timezone_set('Asia/Tehran');

$return = '';

mysql_connect($host,$user,$pass) or die('Connection error');
mysql_select_db($name) or die('Database error');
mysql_query('SET NAMES \'utf8\'');
mysql_set_charset('utf8');

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

foreach($tables as $table) {
$result = mysql_query('SELECT * FROM `'.$table.'`');
$num_fields = mysql_num_fields($result);
$return .= 'DROP TABLE IF EXISTS `'.$table.'`;'.PHP_EOL.PHP_EOL;
$row = mysql_fetch_row(mysql_query('SHOW CREATE TABLE `'.$table.'`'));
$return .= $row[1].';'.PHP_EOL.PHP_EOL;

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] = str_replace('\n', '\\n', $row[$j]);
if (isset($row[$j])) {
$return .= '\''.$row[$j].'\'';
}
else {
$return .= '\'\'';
}
if ($j < ($num_fields - 1)) {
$return .= ',';
}
}
$return .= ');'.PHP_EOL;
}
}
$return .= PHP_EOL.PHP_EOL.PHP_EOL;
}



header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="db-backup-' . $name . '-' . date('Y,m,d-H,i,s').'.sql"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.mb_strlen($return, 'utf-8'));
ob_clean();
flush();
echo $return;
}

backup_db('localhost', '******', '*****', '****');
?>

مهرداد سیف زاده
شنبه 11 مرداد 1393, 23:44 عصر
خط 65 میاد و متغیر return رو echo‌میکنه که با توجه به تنظیم header مقدار خروجی به ظکل فایل دانلود میشه
برای ذخیره توی فایل اونم توی مسیر جاری از خط ۵۵ تا ۶۵ رو بصورت زیر ویرایش کنیم


file_put_contents('db_backup.sql',$return);


یعنی بجای این ده خط دستور بالا رو بزارید

ruhy
دوشنبه 13 مرداد 1393, 23:34 عصر
<?php
function backup_db($host, $user, $pass, $name, $tables = '*') {
date_default_timezone_set('Asia/Tehran');

$return = '';

mysql_connect($host,$user,$pass) or die('Connection error');
mysql_select_db($name) or die('Database error');
mysql_query('SET NAMES \'utf8\'');
mysql_set_charset('utf8');

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

foreach($tables as $table) {
$result = mysql_query('SELECT * FROM `'.$table.'`');
$num_fields = mysql_num_fields($result);
$return .= 'DROP TABLE IF EXISTS `'.$table.'`;'.PHP_EOL.PHP_EOL;
$row = mysql_fetch_row(mysql_query('SHOW CREATE TABLE `'.$table.'`'));
$return .= $row[1].';'.PHP_EOL.PHP_EOL;

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] = str_replace('\n', '\\n', $row[$j]);
if (isset($row[$j])) {
$return .= '\''.$row[$j].'\'';
}
else {
$return .= '\'\'';
}
if ($j < ($num_fields - 1)) {
$return .= ',';
}
}
$return .= ');'.PHP_EOL;
}
}
$return .= PHP_EOL.PHP_EOL.PHP_EOL;
}
file_put_contents('db_backup.sql',$return);
}

backup_db('localhost', '*****', '*******', '******');
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$from_mail."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
$my_file = "db_backup.sql";
$my_path = $_SERVER['DOCUMENT_ROOT']."/";
$my_name = "raj";
$my_mail = "info@domain.com";
$my_subject = "Email Subject ";
$my_message = "Refer the attached file.";
$to_email="civilparto@yahoo.com";

mail_attachment($my_file, $my_path, $to_email, $my_mail, $my_name, $my_subject, $my_message);
unlink('db_backup.sql');
?>


من ایند نوشتم وقتی دستی اجراش میکنم فایل به درستی ایمیل میشه اما وقتی کرون جاب میزارم فایل رو خالی اتچ میکنه


دوستان نظرتون چیه

ruhy
چهارشنبه 15 مرداد 1393, 12:29 عصر
دوستان لطفا راهنمایی بفرمایند تو کرون جاب فایل خالی اتچ میشه لطفا راهنمایی کنید

ruhy
پنج شنبه 16 مرداد 1393, 19:44 عصر
چجوری میتونم چک کنم که فایل ایجاد شده یا نه

ruhy
جمعه 17 مرداد 1393, 10:08 صبح
با curl مشکلم حل شد و کاملا درست کار کرد