创建一个临时文件夹以保持文件在结构中,并通过压缩文件夹使其可下载 [英] Create a temporary folder to keep the files in structure and make it downloadable by compressing the folder

查看:212
本文介绍了创建一个临时文件夹以保持文件在结构中,并通过压缩文件夹使其可下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一些与服务器上的项目相关的文件,如下所示:

  Item-1 => file1.txt,file2.pdf和file3.doc 
Item-2 => file4.pdf,file5.ppt
Item-3 => file6.txt,file7.docx和file8.ppt
....



通过 $ this-> getAllItems()获取上述项目,然后循环遍历每个项目。



试图以结构化的方式压缩所有这些文件。所以,用户可以理解文件属于哪些项目。



我想做的是将与他们尊重的项目相关的文件保存在一个文件夹中,并将其包装一个主文件夹(这应该发生在一个临时路径),然后压缩它,让用户下载compress文件夹。因此,文件将采用如下所示的结构化方式:

  main 
Item-1
- > file1.txt
- > file2.pdf
- > file3.doc
Item-2
- > file4.pdf
- > file5.ppt
Item-3
- > file6.txt
- > file7.docx
- > file8.ppt

在这里搜索时,我得到了一个,它在临时路径中压缩文件(但不是文件夹),并使压缩文件夹可供下载。



PS:用户每次下载请求时应进行压缩。

解决方案

我不确定这是否正确..但是这是工作。



我修改了代码的for循环部分, a href =http://stackoverflow.com/a/13930314/1577396>链接

 #循环遍历每个文件
foreach($ files as $ file){

#下载文件
$ download_file = file_get_contents($ file);

#创建一个空文件夹
$ zip-> addEmptyDir($ itemName);

#add到上面创建的zip文件夹
$ zip-> addFromString($ itemName。'/'。basename($ file),$ download_file);

}

在上面的代码中, $ itemName 是循环在 $ this-> getAllItems()中动态获取的项目名称,如我在我的帖子中提到的。 / p>

请让我知道这是否正确。


In my project, I have some files related to items on Server as shown below:

Item-1  => file1.txt, file2.pdf and file3.doc
Item-2  => file4.pdf, file5.ppt
Item-3  => file6.txt, file7.docx and file8.ppt
....

I can get the above items by $this->getAllItems() and then loop through each item.

I am trying to compress all these files in a structured way. So, that the user can understand files belong to which items.

What I am trying to do is keep the files related to their respected items in a folder and wrap them in a "main" folder (this should happen in a temporary path) then Compress it and let the user download the compress folder. So, that the files will be in structured way as shown below:

main
   Item-1
      --> file1.txt
      --> file2.pdf
      --> file3.doc
   Item-2
      --> file4.pdf
      --> file5.ppt
   Item-3  
      --> file6.txt
      --> file7.docx
      --> file8.ppt

While searching here, I got a link which does compress the files (but not folders) in a temporary path and make the compressed folder available to download.

PS: The compressing should happen for every download request by users.

解决方案

I am not sure whether this is correct or not.. but this worked.

I modified the for loop part of the code which is available on this link.

# loop through each file
foreach($files as $file){

    # download file
    $download_file = file_get_contents($file);

    # Create a empty folder
    $zip->addEmptyDir($itemName);

    #add to the above created new folder in zip
    $zip->addFromString($itemName . '/' . basename($file), $download_file);

}

In the above code, $itemName is the item name which is fetched dynamically while looping in $this->getAllItems() as I had already mentioned in my post.

Please let me know if this is correct or not.

这篇关于创建一个临时文件夹以保持文件在结构中,并通过压缩文件夹使其可下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆