简体   繁体   English

如何在php(yii)中以zip格式下载文件

[英]How to download files in zip in php (yii)

How to download user uploaded files in php yii framework? 如何在php yii框架中下载用户上传的文件?

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.. 在上载文件夹中,还有许多其他文件夹,这些文件夹以用户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. 1,2,3是user1,user2,user3 ..的文件夹名称,而1,2,3是user1,user2,user3的id

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. 所有用户文件的每个部分均保存在其文件夹中。.现在,我想在CgridView中为每个用户的删除按钮附近添加一个下载按钮..我想为我想要的任何用户以zip下载所有文件。

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. 我是yii的新手,不知道如何获取每个用户的所有文件并下载到zip文件夹中。 any suggestions. 有什么建议么。

Step 1: You need to generate associate link in your grid to call a Yii controller -> action, let say actionZipDownload. 步骤1:您需要在网格中生成关联链接,以调用Yii控制器-> action,比如说actionZipDownload。 For this, follow the link below: Make sure you also pass the userId to that action. 为此,请单击下面的链接:确保还将userId传递给该操作。

http://www.yiiframework.com/wiki/593/yii-cgridview-add-custom-button/ 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). 步骤2:在基于传递的userId的操作上,您需要根据您的设置(目录结构)将目标关联用户目录路径。

Step 3: Read all the files from that particular directory, add those for zip creation and store the zip file in a temporary directory. 步骤3:从该特定目录中读取所有文件,添加用于zip创建的文件,并将zip文件存储在一个临时目录中。 zip name can be userId_date.zip 邮政编码名称可以是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. 第2步和第3步的源可能如下所示,您需要对$ destination的相对路径进行一些调整。 Also check php doc for http://php.net/manual/en/class.ziparchive.php 另外,请检查php文档中的http://php.net/manual/zh/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. 步骤4:成功生成zip后,将响应发送到浏览器以进行下载。

Step 5: After successful download you might need to delete the temp zip archive to save space. 步骤5:成功下载后,您可能需要删除temp zip存档以节省空间。

From step 2 to step 5, follow this link http://www.bsourcecode.com/yii-framework/yii-files/yii-zip-format/ 从步骤2到步骤5,点击此链接http://www.bsourcecode.com/yii-framework/yii-files/yii-zip-format/

Hope helps. 希望会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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