-1

I have a situation, I have list of user data, in that data details, i have stored a file also. Now i want to download all the file of users in zip file, when a user click on a button like DOWNLOAD.

i have written a action in controller where im fetching the file to be downloaded, but im unable to write code for making zip finle and download it. Below is my code in controller.

public function actionMain($jid)
{
     $values = UserRequest::find()->where(['sender_code'=>$jid,'status'=>1])->all();
       $i=1; foreach($values as $data) { 
             $users = Users::find()->where(['access_code'=>$data->reciever_code])->one();
             $file_names[]= $users->attach_cv;
         }
 }

This is working fine. in $file_name im fetching all the file's, But i want to download this files in a zip foder.

Please help me to solve this issue

The link provided is in core php and easily implemented in that. But how about doing the same in framework. Because i have array of file name in action. As you can see above. DO i need to write the code for generating .ZIP in the same action or any other action.

Salman Riyaz
  • 808
  • 2
  • 14
  • 37
  • Possible duplicate of [ZIP all files in directory and download .zip generated](http://stackoverflow.com/questions/17708562/zip-all-files-in-directory-and-download-zip-generated) – SiZE Mar 19 '17 at 05:32
  • No tat solution is in core php. I need to do in Yii2.0 framework. – Salman Riyaz Mar 19 '17 at 06:56
  • Please explain, in the answer itself, why you cannot use . Also, if the whole point is generating a ZIP file from multiple data files, you should probably shorten your question and drop all the irrelevant details - unless you want to bypass the individual file generation altogether, in which case make _that_ clear instead. – einpoklum Mar 19 '17 at 18:38
  • I want to generate and download the file at the same time. so tats where im facing the problem. – Salman Riyaz Mar 20 '17 at 06:29

1 Answers1

0

I would recommend using a ZipArchive as specified in the comment by SiZE. That way you initiate the zipping process before you start the download.

File creation:

/**
 * Generate zip file for upload
 *
 * @throws Exception
 */
private function createZip($files)
{
    $file = 'full_path_to_file/your_file_name.zip';

    $zip = new ZipArchive();
    if ($zip->open($file, ZipArchive::CREATE) !== TRUE) {
        throw new \Exception('Cannot create a zip file');
    }

    foreach($files as $file){
        $zip->addFile($file[file_name], $file[local_name]);
    }

    $zip->close();

}

And then you can return the zip file path for download in your action

Community
  • 1
  • 1
user2831723
  • 832
  • 12
  • 25