Avatar of Richard Korts
Richard Korts
Flag 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

PHP* zipped files

Avatar of undefined
Last Comment
Scott Fell

8/22/2022 - Mon
Scott Fell

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 https://github.com/alexcorvi/php-zip 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.

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?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Richard Korts


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


Richard Korts

I meant final file names.

Richard Korts

Hello, out there!

Does anyone have anything that can help me with this?

Thank you.

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Scott Fell

Richard Korts


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.


Scott Fell

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question