问题
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