Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. public static function backup($tables, $file)
  2. {
  3. $return = ''; // LINE 1221
  4. $file_written = false;
  5.  
  6. //get all of the tables
  7. if ($tables == '*') {
  8. $tables = array(); // LINE 1226
  9. $result = mysqli_query(self::$cn, 'SHOW TABLES');
  10. while ($row = mysqli_fetch_row($result)) {
  11. $tables[] = $row[0];
  12. }
  13. } else {
  14. $tables = is_array($tables) ? $tables : explode(',', $tables);
  15. }
  16.  
  17. // Initialize File
  18. $filename = $file . 'db-backup-' . time() . '-' . (md5(implode(',', $tables))) . '.sql';
  19. echo $filename; die(); // placed this here so I could see
  20. // if it was running out of memory above
  21. // and this echos out fine, so the problem
  22. // is somewhere below.
  23. $handle = fopen($filename, 'w');
  24. fclose($handle);
  25. $handle = fopen($filename, 'a');
  26.  
  27. //cycle through
  28. foreach ($tables as $table) {
  29. if ($table <> "") {
  30. $result = mysqli_query(self::$cn, 'SELECT * FROM `' . $table . '`');
  31. if ($result) {
  32. $num_fields = mysqli_num_fields($result);
  33.  
  34. $row2 = mysqli_fetch_row(mysqli_query(self::$cn, 'SHOW CREATE TABLE `' . $table . '`'));
  35. if ($row2) {
  36. $file_written = true;
  37. $return .= 'DROP TABLE IF EXISTS `' . $table . '`;';
  38. $return .= "nn" . $row2[1] . ";nn";
  39.  
  40. $field_flags = false;
  41. fwrite($handle, $return);
  42. $return = '';
  43.  
  44. for ($i = 0; $i < $num_fields; $i++) {
  45. while ($row = mysqli_fetch_row($result)) {
  46.  
  47. if ($field_flags == false) {
  48. for ($j = 0; $j < $num_fields; $j++) {
  49. $tmp = mysqli_fetch_field_direct($result, $j);
  50. if (strpos($tmp->flags, 'NOT_NULL') !== false) {
  51. $field_flags[$j] = false;
  52. } else {
  53. $field_flags[$j] = true;
  54. }
  55. }
  56. }
  57.  
  58. // var_dump($field_flags); die();
  59. $return .= 'INSERT INTO `' . $table . '` VALUES(';
  60. for ($j = 0; $j < $num_fields; $j++) {
  61. $row[$j] = addslashes($row[$j]);
  62. $row[$j] = preg_replace("/n/", "\n", $row[$j]);
  63. if (isset($row[$j])) {
  64. if ($row[$j] <> '') {
  65. $return .= '"' . $row[$j] . '"';
  66. } else {
  67. if ($field_flags[$j]) {
  68. $return .= "NULL";
  69. } else {
  70. $return .= '""';
  71. }
  72. }
  73. } else {
  74. if ($field_flags[$j]) {
  75. $return .= "NULL";
  76. } else {
  77. $return .= '""';
  78. }
  79. }
  80. if ($j < ($num_fields - 1)) {
  81. $return .= ',';
  82. }
  83. }
  84. $return .= ");n";
  85. fwrite($handle, $return);
  86. $return = '';
  87. }
  88. }
  89. $return .= "nnn";
  90. fwrite($handle, $return);
  91. $return = '';
  92. }
  93. }
  94. mysqli_free_result($result);
  95. }
  96. }
  97. fclose($handle);
  98.  
  99. //save file
  100. if ($file_written) {
  101. return $filename;
  102. } else {
  103. if (file_exists($filename)) {
  104. unlink($filename);
  105. }
  106. return false;
  107. }
  108. }
  109.  
  110. PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32 bytes) in d:serverhtdocsbackup.php on line 1221, referer: http://example.com/index.html
  111. PHP Stack trace:, referer: http://example.com/index.html
  112. PHP 1. {main}() d:serverhtdocsbackup.php:0, referer: http://example.com/index.html
  113. PHP 2. mysqli_db::backup() d:serverhtdocsbackup.php:31, referer: http://example.com/index.html
  114. PHP 3. mysqli_fetch_field_direct() d:serverhtdocsbackup.php:1221, referer: http://example.com/index.html
  115. PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32 bytes) in d:serverhtdocsbackup.php on line 1226, referer: http://example.com/index.html
  116.  
  117. $result = mysqli_query(self::$cn, 'SELECT * FROM `' . $table . '`', MYSQLI_USE_RESULT);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement