I have the following file structure. Some files are dynamically added to a zip file and downloaded.

uploads
----- 1
---------- test.jpg
---------- another-test.jpg
----- 2
---------- yetanotherfile.jpg
----- 3
---------- evenmorefiles.jpg

However, when I unzip the downloaded file it keeps that same structure (only with some files missing) and the unzipped folder is called upload.
How can I:

  • Change the name of the unzipped folder
  • Get rid of the structure

My code for generating the file is

if ($_GET['download']== 'true'){
          $downloadarray = array();
          while($row = mysql_fetch_array($res)){   
             $downloadarray[] =  $row['loc'] ;
          }
          $zipname = rand(0,9999) . 'download.zip';
          $zip = new ZipArchive;
          $zip->open($zipname, ZipArchive::CREATE);
          foreach ($downloadarray as $file) {
            $zip->addFile($file);
          }
          $zip->close();
          header('Content-Type: application/zip');
          header('Content-disposition: attachment; filename=' . $zipname);
          header('Content-Length: ' . filesize($zipname));
          readfile($zipname);
          unlink($zipname);

        }

Thanks for any help!

Member Avatar for LastMitch

However, when I unzip the downloaded file it keeps that same structure (only with some files missing) and the unzipped folder is called upload.How can I:

I assume the code works. I dont see anything wrong in the code that you provided.

Change the name of the unzipped folder

You mention the folder is called upload. I don't see in the code you provided has a word upload?

Get rid of the structure

What do you mean by structure? You mean compressed?

Try to add this to your code:

$FilesToZip = array($file1,$file2);
$ZipName = './' . strval(rand(0,9999)) . '.zip';
create_zip($FilesToZip, $ZipName);

as for your header add this:

 header('Content-Type: application/zip');
 header('Content-Description: Zipped File Transfer');
 header('Content-Disposition: attachment; filename='.basename($ZipName));
 header('Content-Transfer-Encoding: binary');
 header('Content-Length: ' .filesize($ZipName));

You mention the folder is called upload. I don't see in the code you provided has a word upload?

I mean uploads, sorry.
What the problem is that it keeps the original folder structure. I want it to just put all of the files in one zip file and I want to be able to change the name of the unzipped folder.
The current structure when unzipped is that of my first post but I want it to be:

download.zip:
download
----- test.jpg

ect...

How can I just copy the file itself but not the folder structure and how can I rename the folder that is unzipped

Member Avatar for LastMitch

I want it to just put all of the files in one zip file and I want to be able to change the name of the unzipped folder.

This code will zip and rename the zip folder:

$FilesToZip = array($file1,$file2);
$ZipName = './' . strval(rand(0,9999)) . '.zip';
create_zip($FilesToZip, $ZipName);

How can I just copy the file itself but not the folder structure and how can I rename the folder that is unzipped

The structure I mean it's a zip folder. I don't think you can choose which file goes first or in order. I used PeaZip not related to php. It's software and I can't even organized which files that I want to begin with or in order. Once it's unzip, it will display all the files not in order.

Where did you get this code? Did you write this? I don't see anywhere in your code you provided explain how to reorganized the files nor gather and then zip. I only see that your code just zip files not rearrange.

Instead of this:

foreach ($downloadarray as $file) {
$zip->addFile($file);
}

Add another $file here:

foreach($downloadarray as $file) {
$zip->addFile($file,$file);
}

Then your code to zip is this:

$FilesToZip = array('images/1.jpg','images/2.jpg','images/5.jpg','name/6.gif','root.doc','root.pdf');
$ZipName = './' . strval(rand(0,9999)) . '.zip';
$result = create_zip($FilesToZip, $ZipName);

What I want to do is create a zip file with all of the files in the root directory, not in individual folders.

Worked it out myself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.