Search
 
SCRIPT & CODE EXAMPLE
 

PHP

Download multiple files as zip in PHP

<?php

/* create a compressed zip file */
function createZipArchive($files = array(), $destination = '', $overwrite = false) {

   if(file_exists($destination) && !$overwrite) { return false; }

   $validFiles = array();
   if(is_array($files)) {
      foreach($files as $file) {
         if(file_exists($file)) {
            $validFiles[] = $file;
         }
      }
   }

   if(count($validFiles)) {
      $zip = new ZipArchive();
      if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) == true) {
         foreach($validFiles as $file) {
            $zip->addFile($file,$file);
         }
         $zip->close();
         return file_exists($destination);
      }else{
          return false;
      }
   }else{
      return false;
   }
}

$fileName = 'myzipfile.zip';
$files = array('uploads/profile1.jpeg', 'uploads/profile2.jpeg');
$result = createZipArchive($files, $fileName);

header("Content-Disposition: attachment; filename="".$fileName.""");
header("Content-Length: ".filesize($fileName));
readfile($fileName);

?>
Comment

how to make zip in php by multiple files

$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files 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);
Comment

lDownload multiple files as a zip-file using php

$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
  $zip->addFile($file);
}
$zip->close();
Comment

PREVIOUS NEXT
Code Example
Php :: carbon check if date is greater 
Php :: centos 8 laravel permission denied 
Php :: php check if string contains url 
Php :: Delete quotes in php 
Php :: wp_customize_image_control 
Php :: composer remove phpmailer 
Php :: Enqueue WordPress Scripts and Styles 
Php :: php get keys of duplicate values in array 
Php :: display image from mysqli database 
Php :: wordpress disable block styles 
Php :: php array order alphabetically 
Php :: php if elseif g 
Php :: Creating default object from empty value 
Php :: how hide empty category woocommerce wordpress 
Php :: php logout 
Php :: laravel update all relations 
Php :: is replace case sensitive php 
Php :: min function in php 
Php :: php array get value at index 
Php :: check array is associative laravel 
Php :: php array_walk 
Php :: php sprintf 
Php :: axios post not sending data php 
Php :: maintenance mode laravel 
Php :: __invoke in laravel 
Php :: file_get_contents php 
Php :: convert datetime to string in php 
Php :: get date after 1 dayphp 
Php :: php header x forwarder for 
Php :: laravel permissions 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =