0

I am looking to add both folders and files from a matching array to a zip file. Below is my code:

function listdir($start_dir='./assets') {
  //Array that will contain the director
  $files = array();

  if (is_dir($start_dir)) {
    $fh = opendir($start_dir);
    while (($file = readdir($fh)) !== false) {
      // Loop through the files, skipping '.' and '..', and recursing if necessary
      if (strcmp($file, '.')==0 || strcmp($file, '..')==0) continue;
      $filepath = $start_dir . '/' . $file;
      if ( is_dir($filepath) )
        $files = array_merge($files, listdir($filepath));
      //else
        array_push($files, $filepath);
    }
    closedir($fh);
  } else {
    // false if the function was called with an invalid non-directory argument
    $files = false;
  }
  return $files;
}

//Array of all files
$allFiles = listdir('./assets');
print_r($allFiles);

//Gets all values with name "asset"
$name = $_POST['asset'];

//If there are items in the array, zip them together
if($name!=0){
  //Compares $_POST array with array of all files in directory
  $result = array_intersect($allFiles, $name);

  function zipFilesAndDownload($result){
  $zip = new ZipArchive();
  //create the file and throw the error if unsuccessful
  if ($zip->open('SelectedAssets.zip', ZIPARCHIVE::CREATE )!==TRUE) {
      exit("cannot open SelectedAssets.zip\n");
  }
  //add each files of $file_name array to archive
  foreach($result as $allFiles)  {
      $zip->addFile($allFiles);     
  }

  $zip->close();
  $zipped_size = filesize('SelectedAssets.zip');
  header("Content-Description: File Transfer");
  header("Content-type: application/zip"); 
  header("Content-Type: application/force-download");// some browsers need this
  header("Content-Disposition: attachment; filename=SelectedAssets.zip");
  header('Expires: 0');
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Pragma: public');
  header("Content-Length:". " $zipped_size");
  ob_clean();
  flush();
  readfile("SelectedAssets.zip");
  unlink("SelectedAssets.zip"); // Now delete the temp file (some servers need this option)
      exit;   
    }
  if(isset($_POST['submit'])) {
  //$file_names=$_POST['assets'];// Always sanitize your submitted data!!!!!!
  //$file_names = filter_var_array($_POST['assets']);//works but it's the wrong method
  $filter = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS) ;
  $file_names = $filter['assets'] ; 
  //Archive name
  $archive_file_name='DEMO-archive.zip';
  //Download Files path
  $file_path= getcwd(). './';
  //call the function
  zipFilesAndDownload($result);
  } else {

  print 'Something went wrong.';
  exit;
  }
} //Otherwise, don't.
else {
  print_r("Please select a file.");
}

The code above searches through a directory and reads all the folders and files. Then using array_intersect I matched files that were put into an empty array by the user to all the files in the previously searched directory. I then zip the files that match and download.

My question is, how can I have a folder be added to this zip as well? As of now, this only adds files and folders are assumed as empty.

  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jan 18 '17 at 21:07
  • I'm in kind of a drive-by mode at the moment, but this might be a duplicate that will help you: [http://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php](http://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php) – TecBrat Jan 18 '17 at 21:07

0 Answers0