Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

asked on

Creating a zip file in php in a specific way

I am using the Zip Archive Class to create zip files of documents related to the outcome of a specific "configuration" where the user creates a mockup of a system he may choose to buy from the company and based on the users choices, specific support documents can be asked to be downloaded. The names of the documents themselves do not reveal their content. For example, a product brochure document might be ABC-XYZ-00987.pdf. To make the contents of the created zip file meaningful to the user, we want to create a subfolder named like 43210files where 43210 is the unique visitor ID of the user running this session. Within the subfolder, we want to create other sub folders, like Brochure, IO Manual, Summary, Product Drawing, etc. Then the exact required document will be copied into that sub sub folder. So, for example, we will have this subfolder under the directory level of the executing download program creating the zip file: downloads/43210files/Brochure/<actual file name>. Typically there will be 3 to 8 files per download.

What we want the zip file to look like is to include the sub sub directory name (like Brochure/file_name but NOT the "downloads/43210files.

What is the best way to do this? Note, we do not HAVE to use the php class Zip Archive if there is a different or better way.

Thank you

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

It looks like you have several parts. Which part(s) do you need to work on?

1) A logged in user selects a product/item can be one or multiple
  • Each item has specified files associated with it.
  • Display list of items selected and associated files with perhaps a checkbox to select files to get downloaded. 

2) Grab selected files to be downloaded, create one zip file made up of those files
  • Filename of the zip is based on the id of the logged in user.
  • Zip file should be downloadable by the user.

The easy part is zipping up the file. If you have a class like it is a matter of using their example and making it your own.  This seems like the easy part.
 require_once "path/to/src/zip.php";
    $zip = new Zip();
    $zip->zip_add("path/to/example.png"); // adding a file
    $zip->zip_add(array("path/to/example1.png","path/to/example2.png")); // adding two files as an array
    $zip->zip_add(array("path/to/example1.png","path/to/directory/")); // adding one file and one directory

Open in new window

How are the files that can potentially be downloaded associated with the items the user is picking out? By database? some other means?  Is this one of the parts you need help with?

Does the final zip file need to be saved on the server for a specified length of time for anybody that has the link to download?  Or would it be better to keep the files outside of the public www as well as the generated zip file. Then once the zip is created, stream it down to the user. Once they download it, there is no link to keep downloading it by accident. You can still create a link like <a class="download" id="downloadlink43210" href="43210  ">43210</a> and via an ajax request stream the file down.  That way you are not keeping files on the public www.

Avatar of Richard Korts


Scott, I will shortly show you what I am doing now & what it produces that we want to simplify.

I knew I should have used troubleshoot and not what I did.

I am aware of putting the files NOT into the pubic-html area, I did not what to throw that complexity into this because I did not want to confuse the essence. Which I obviously did.


What I do is to save each files in DB (real name, epox name, userid, file type, size, date...)
What I do is  I rename the file using epox to make sure all files have unique name.
I save the real name and the epox name in DB.
Using userid as directory name is what I ususally do.
When the user download the file, I rename the file using PHP with the real name so the process is transparent to the end user. (I clean a bit the file name if required let say some special characters that can cause issue).

About zip what does trigger the zip?
Are you sending this zip by mail, direct download, copy to cloud using S3?

Here is what I am doing now. I pass a bunch of files to a download program in the query string, files separated by a ~.

The download program is attached.

Also attached is a sample of the existing zipfile I create. Note that there are too many levels of folders in the zip file; zip(29), downloads, 46895files.

What I want is for the zip file to contain, directly without any subfolder structure, the final file name only.

How can I change the code to make that happen?

Thank you


I meant final file names.

Hello, out there!

Does anyone have anything that can help me with this?

Thank you.


I did not follow what that provided that helped.

Here is an example of the issue.

I have a file named xyz.php. The name is more complex than that, but the name does not convey that the file is. I want to precede that file with, for example, "product_brochure/", that being a name conveying meaning.

I want the zip file to have within it elements like "product_brochure/xyz.pdf", "spec_drawng/abc.pdf", etc.

But now I get, in the zip file, a structure like "downloads/doc_files/product_brochure/xyz.pdf.

I want to eliminate those "upper level" folder structures (downloads/doc_files/") from the inner part of the zip. It is confusing to the end customer to have all that preliminary folder structure to have to navigate down to to get the files they want.


Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial