0

I want to download files that I have uploaded on the server, in a .zip file using PHP. The files are not placed in the same folder.

The folders are named like this : idClub.'-'.idEvent.'-'.idOrderItem.

IdClub and idEvent are always the same, and it's idOrderItem that change.

We can get orderItem ids from this query :

select idOrderItem from order_item when idEvent = $idEvent

$idEvent : is a parameter given to the function that downloads the files.

So is the there an example of script ?

user2285831
  • 419
  • 1
  • 5
  • 18
  • 3
    Possible duplicate of [Creating .zip file : PHP](http://stackoverflow.com/questions/11540339/creating-zip-file-php) – DevDonkey Apr 11 '16 at 11:51

1 Answers1

1

Hope something like this works:

$query= "select idOrderItem from order_item when idEvent = ".$idEvent;
if ($result = $mysqli->query($query)) {
    $zip = new ZipArchive;

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
         $folder_path = '/path/to/idClub-idEvent-'.$row['idOrderItem'].'/';
         if ($zip->open('yourzipname.zip') === TRUE) {
             foreach (glob($folder_path."*.*") as $filename) {
                 $zip->addFile($folder_path.$filename, $filename);
             }
         }
    }
    $zip->close();        
 }
VipindasKS
  • 516
  • 7
  • 15
  • I don't know the file names, they are supposed to be dynamically in the addFile command, so how can we resolve this ? – user2285831 Apr 11 '16 at 13:20
  • you can use glob function in php to scan the directory and read file names. Please see the updated answer. – VipindasKS Apr 12 '16 at 06:52