Asfi Tanoli Asfi Tanoli - 2 years ago 200
PHP Question

How to download files in zip in php (yii)

How to download user uploaded files in php yii framework?

I have list of users on my website, every user can upload multiple files for each sections(Work Information, Education, Certificates etc)

The uploaded files are being saved in uploads files. Here is the link


in uploads foler, there are many other folders, folders are named with user id..


1,2,3 is name of folder for user1, user2, user3.. and 1,2,3 is a
of user1, user2, user3.

When user first upload anyfile and save it, then the folder for that user is created in uploads folders.

All users files are each section are saved in their folder.. Now I want to add a download button in CgridView near the delete button for each user.. and i want to download all the files in zip for any user i want.

Any example code of doing it? What tool, extension or library should i use? or is there any code available? im new to yii, have no idea how to get all files for each user and download in zip folder. any suggestions.

Answer Source

Step 1: You need to generate associate link in your grid to call a Yii controller -> action, let say actionZipDownload. For this, follow the link below: Make sure you also pass the userId to that action.

Step 2: on that action based on passed userId you need target associate user directory path according to your setup (directory structure).

Step 3: Read all the files from that particular directory, add those for zip creation and store the zip file in a temporary directory. zip name can be

The source probably for step 2 and 3 would be like below, you need to do some adjustment to the relative path as $destination. Also check php doc for

public function actionZipDownload(){
$userId = $_GET['userId']; // get the passed user id
$zip=new Zip Archive();
if($zip->open($destination,ZIPARCHIVE::CREATE) !== true) {
  return false;
 foreach($doclist as $thefile)

Step 4: after successful generation of the zip, send the response to browser for download.

Step 5: After successful download you might need to delete the temp zip archive to save space.

From step 2 to step 5, follow this link

Hope helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download