Friday, 2 September 2016

yii2 create zip file

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(); }
 

yii2 create zip file

3 comments:

  1. 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

    Thanks
    Ravi
    Bangalore

    ReplyDelete
    Replies
    1. Hi Ravi
      Pls Check Backend/web folder & frontend/web folder

      Code Paste in Controller and run browser ..

      Thanks & Regards
      Hemant Vishwakarma

      Delete
    2. I am checked Code is correct

      Try Again .............

      Thanks & Regards
      Hemant Vishwakarma

      Delete