Create a temporary folder to keep the files in structure and make it downloadable by compressing the folder

不想你离开。 提交于 2019-12-13 06:37:00

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/23052665/create-a-temporary-folder-to-keep-the-files-in-structure-and-make-it-downloadabl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!