Last active
October 3, 2019 12:28
-
-
Save Guley/a1fdbaad96005126103ffeb7afab68dc to your computer and use it in GitHub Desktop.
Export or dump your database tables on one click in codeigniter 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('BASEPATH') or exit('No direct script access allowed'); | |
class Dump extends CI_Controller | |
{ | |
public function index() | |
{ | |
$this->load->dbutil(); | |
$tableList=$this->db->query("SELECT t.TABLE_NAME AS tablename FROM INFORMATION_SCHEMA.TABLES AS t WHERE t.TABLE_SCHEMA = 'database_name'")->result_array(); | |
if ($this->dbutil->database_exists('database_name')) | |
{ | |
if(!empty($tableList)){ | |
$currentTime = date('Y-m-dH:i:s'); | |
$foldername = 'backup_'.$currentTime; | |
$upload_path = APPPATH.'/cache/'.$foldername; | |
if (!is_dir( $upload_path)) { | |
mkdir( $upload_path, 0777, TRUE); | |
} | |
foreach ($tableList as $table) { | |
$prefs = array( | |
'tables' => [$table['tablename']], | |
'format' => 'sql', | |
'add_drop' => TRUE, | |
'add_insert' => TRUE, | |
'newline' => "\n" | |
); | |
$backup = $this->dbutil->backup($prefs); | |
$this->load->helper('file'); | |
write_file($upload_path.'/'.$table['tablename'].'.sql', $backup); | |
} | |
// Get real path for our folder | |
// Initialize archive object | |
$zip = new ZipArchive(); | |
$zip->open($upload_path.'.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE); | |
// Create recursive directory iterator | |
/** @var SplFileInfo[] $files */ | |
$files = new RecursiveIteratorIterator( | |
new RecursiveDirectoryIterator($upload_path), | |
RecursiveIteratorIterator::LEAVES_ONLY | |
); | |
foreach ($files as $name => $file) | |
{ | |
// Skip directories (they would be added automatically) | |
if (!$file->isDir()) | |
{ | |
// Get real and relative path for current file | |
$filePath = $file->getRealPath(); | |
$relativePath = substr($filePath, strlen($upload_path) + 1); | |
// Add current file to archive | |
$zip->addFile($filePath, $relativePath); | |
} | |
} | |
// Zip archive will be created only after closing object | |
$zip->close(); | |
$this->deleteDir($upload_path); | |
$this->load->helper('download'); | |
$file_content = file_get_contents($upload_path.'.zip'); // Read the file's contents | |
if(file_exists($upload_path.'.zip')){ | |
unlink($upload_path.'.zip'); | |
} | |
force_download($foldername.'.zip', $file_content); | |
} | |
} | |
} | |
public function deleteDir($dirPath) { | |
if (! is_dir($dirPath)) { | |
throw new InvalidArgumentException("$dirPath must be a directory"); | |
} | |
if (substr($dirPath, strlen($dirPath) - 1, 1) != '/') { | |
$dirPath .= '/'; | |
} | |
$files = glob($dirPath . '*', GLOB_MARK); | |
foreach ($files as $file) { | |
if (is_dir($file)) { | |
self::deleteDir($file); | |
} else { | |
unlink($file); | |
} | |
} | |
rmdir($dirPath); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment