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

1

0

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.

Booklover answered 14/4, 2014 at 5:36 Comment(1)
I don't know much about compressing and creating directories in temporary path.. so, I have no code but just a link to refer what I had tried.Booklover
B
0

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.

Booklover answered 14/4, 2014 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.