简体   繁体   中英

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

C:\wamp\www\project\themes\mydesign\assets\global\uploads

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

C:\wamp\www\project\themes\mydesign\assets\global\uploads\1\
C:\wamp\www\project\themes\mydesign\assets\global\uploads\2\
C:\wamp\www\project\themes\mydesign\assets\global\uploads\3\

1,2,3 is name of folder for user1, user2, user3.. and 1,2,3 is a id 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.

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.

http://www.yiiframework.com/wiki/593/yii-cgridview-add-custom-button/

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 userId_date.zip

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 http://php.net/manual/en/class.ziparchive.php

<?php
public function actionZipDownload(){
$userId = $_GET['userId']; // get the passed user id
$zip=new Zip Archive();
$destination=dirname($_SERVER['DOCUMENT_ROOT'])."/project/themes/mydesign/assets/global/uploads/".$userId."/filename.zip";
if($zip->open($destination,ZIPARCHIVE::CREATE) !== true) {
  return false;
}
 $doclist=CUploadedFile::getInstancesByName('files');
 foreach($doclist as $thefile)
 {
  $random=rand(11111,99999);
  $filename=$random.$thefile;      
  $zip->addFile($thefile->tempname,$filename);
 }
 $zip->close();   
 }
 ?>

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 http://www.bsourcecode.com/yii-framework/yii-files/yii-zip-format/

Hope helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM