Function
public function actionFullbackup() { $BackupFileName = 'Placio_'.date('d-m-Y'); // Get real path for our folder $rootPath = realpath(Yii::getAlias('@frontend')."/../"); // Initialize archive object $zip = new \ZipArchive(); $zip->open($BackupFileName.'.zip', \ZipArchive::CREATE | \ZipArchive::OVERWRITE); // Create recursive directory iterator /** @var SplFileInfo[] $files */ $files = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($rootPath), \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($rootPath) + 1); $relativePath = substr($filePath, strlen($rootPath) + 1); // Add current file to archive $zip->addFile($filePath, $relativePath); } } echo $BackupFileName.'.zip'; exit; // Zip archive will be created only after closing object $zip->close(); }
public function actionFullbackup() { $BackupFileName = 'Placio_'.date('d-m-Y'); // Get real path for our folder $rootPath = realpath(Yii::getAlias('@frontend')."/../"); // Initialize archive object $zip = new \ZipArchive(); $zip->open($BackupFileName.'.zip', \ZipArchive::CREATE | \ZipArchive::OVERWRITE); // Create recursive directory iterator /** @var SplFileInfo[] $files */ $files = new \RecursiveIteratorIterator( new \RecursiveDirectoryIterator($rootPath), \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($rootPath) + 1); $relativePath = substr($filePath, strlen($rootPath) + 1); // Add current file to archive $zip->addFile($filePath, $relativePath); } } echo $BackupFileName.'.zip'; exit; // Zip archive will be created only after closing object $zip->close(); }
yii2 create zip file
Hi hemant i tried to apply your code but its taking too much time and then it will return blank page.First execution time error came i extended execution time. please guide me
ReplyDeleteThanks
Ravi
Bangalore
Hi Ravi
DeletePls Check Backend/web folder & frontend/web folder
Code Paste in Controller and run browser ..
Thanks & Regards
Hemant Vishwakarma
I am checked Code is correct
DeleteTry Again .............
Thanks & Regards
Hemant Vishwakarma