Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. class Dump extends CI_Controller
  4. {
  5. public function index()
  6. {
  7. $this->load->dbutil();
  8. $tableList=$this->db->query("SELECT t.TABLE_NAME AS tablename FROM INFORMATION_SCHEMA.TABLES AS t WHERE t.TABLE_SCHEMA = 'database_name'")->result_array();
  9. if ($this->dbutil->database_exists('database_name'))
  10. {
  11. if(!empty($tableList)){
  12. $currentTime = date('Y-m-dH:i:s');
  13. $foldername = 'backup_'.$currentTime;
  14. $upload_path = APPPATH.'/cache/'.$foldername;
  15. if (!is_dir( $upload_path)) {
  16. mkdir( $upload_path, 0777, TRUE);
  17. }
  18. foreach ($tableList as $table) {
  19.  
  20. $prefs = array(
  21. 'tables' => [$table['tablename']],
  22. 'format' => 'sql',
  23. 'add_drop' => TRUE,
  24. 'add_insert' => TRUE,
  25. 'newline' => "\n"
  26. );
  27. $backup = $this->dbutil->backup($prefs);
  28. $this->load->helper('file');
  29. write_file($upload_path.'/'.$table['tablename'].'.sql', $backup);
  30. }
  31.  
  32. // Get real path for our folder
  33.  
  34. // Initialize archive object
  35. $zip = new ZipArchive();
  36. $zip->open($upload_path.'.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
  37.  
  38. // Create recursive directory iterator
  39. /** @var SplFileInfo[] $files */
  40. $files = new RecursiveIteratorIterator(
  41. new RecursiveDirectoryIterator($upload_path),
  42. RecursiveIteratorIterator::LEAVES_ONLY
  43. );
  44.  
  45. foreach ($files as $name => $file)
  46. {
  47. // Skip directories (they would be added automatically)
  48. if (!$file->isDir())
  49. {
  50. // Get real and relative path for current file
  51. $filePath = $file->getRealPath();
  52. $relativePath = substr($filePath, strlen($upload_path) + 1);
  53.  
  54. // Add current file to archive
  55. $zip->addFile($filePath, $relativePath);
  56. }
  57. }
  58.  
  59. // Zip archive will be created only after closing object
  60. $zip->close();
  61. $this->deleteDir($upload_path);
  62. $this->load->helper('download');
  63. $file_content = file_get_contents($upload_path.'.zip'); // Read the file's contents
  64. if(file_exists($upload_path.'.zip')){
  65. unlink($upload_path.'.zip');
  66. }
  67. force_download($foldername.'.zip', $file_content);
  68.  
  69.  
  70.  
  71.  
  72. }
  73. }
  74. }
  75. public function deleteDir($dirPath) {
  76. if (! is_dir($dirPath)) {
  77. throw new InvalidArgumentException("$dirPath must be a directory");
  78. }
  79. if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
  80. $dirPath .= '/';
  81. }
  82. $files = glob($dirPath . '*', GLOB_MARK);
  83. foreach ($files as $file) {
  84. if (is_dir($file)) {
  85. self::deleteDir($file);
  86. } else {
  87. unlink($file);
  88. }
  89. }
  90. rmdir($dirPath);
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement