John Davidson

php - Database Backup function not working Properly in Codeigniter

0 comments
Message:


my normal database size is 32 mb but with this method my database downloaded 97mb and showing error when uploading form localhost/phpmyadmin please tell me what i am doing wrong.


public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url', 'file', 'download'));
$this->load->library(array('session', 'zip'));

$this->db = $this->load->database($_SESSION["db_name"], TRUE);
$this->load->model('My_Model');
}

public function backup(){

// Load the DB utility class
$this->load->dbutil();
$config = array(
'format' => 'zip',
'filename' => $_SESSION["db_name"].'_'.time().'.sql'
);

// Backup your entire database and assign it to a variable
@$backup =& $this->dbutil->backup($config);

write_file('backup/backup_'.date('Y-m-d_H_i_s',time()).'.zip', $backup);

}

phpmyadmin Error




My Answer:
load->dbutil();_ $this->load->helper('file');_}_public function backup(){_ // Load the DB utility class_ $this->load->dbutil();_ // Backup your entire database and assign it to a variable_ $backup =& $this->dbutil->backup();_ // Load the file helper and write the file to your server_ write_file('path/to/directory/backup.sql', $backup);_ // Load the download helper and send the file to your desktop_ $this->load->helper('download');_ force_download('backup.sql', $backup);_}

It seems like you are not compressing the backup file before downloading it. This can result in a larger file size than expected. You can try compressing the backup file before downloading it using the following code:

php
public function backup(){
// Load the DB utility class
$this->load->dbutil();

// Backup your entire database and assign it to a variable
$backup =& $this->dbutil->backup();

// Load the file helper and write the file to your server
write_file('path/to/directory/backup.sql.gz', gzencode($backup));

// Load the download helper and send the file to your desktop
$this->load->helper('download');
force_download('backup.sql.gz', gzencode($backup));
}


This code will compress the backup file using gzip before downloading it, which should reduce the file size. Make sure to change the file extension to `.gz` in both the `write_file` and `force_download` functions.

Rate this post

3 of 5 based on 5827 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us