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

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

问题描述

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

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

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



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



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

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

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



PS:压缩应该由用户每次下载请求发生

解决方案

我不知道这是否正确..但是这样做有效。



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

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

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

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

#添加到以上创建的新文件夹
$ 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天全站免登陆