0

I'm trying to download multiple files from the server in a zipped folder. Here is the code I have thus far:

HTML

<form method="post">
    <input type="checkbox" name="checked[]" value="<?php echo $path; ?>">
    <input type="submit" name="download" value="Download Selected">
</form>

PHP:

if(isset($_POST['download'])){
    $files = $_POST['checked'];
    $zip = new ZipArchive();
    $zip_name = time().".zip"; // Zip name
    $zip->open($zip_name,  ZipArchive::CREATE);
    $full = wp_upload_dir();
    $base = $full['baseurl'] .'/';


foreach ($files as $file) {
  echo $full_path = $file;
  echo $_SERVER['DOCUMENT_ROOT'] . $path;
  if(file_exists($_SERVER['DOCUMENT_ROOT'].'/upload/folder/'.$full_path)){
      $zip->addFromString(basename($base . $full_path),  file_get_contents($base . $full_path));  
  }
  else{
   echo"file does not exist";
  }
}
$zip->close();
}
Michelle M.
  • 515
  • 1
  • 7
  • 20
  • Please have a look to this question http://stackoverflow.com/questions/1754352/download-multiple-files-as-a-zip-file-using-php – Ashkar May 07 '17 at 07:04
  • @Ashkar Yes, I've looked at that thread but can't get the code to work for me. – Michelle M. May 07 '17 at 07:04
  • You gotta add some debugging info... Did you check your server error log files? The answer why it's not working should be there. Also see http://stackoverflow.com/search?tab=votes&q=%5bphp%5d%20debugging – brasofilo May 07 '17 at 13:29
  • Hi @brasofilo thank you for your response. I added the debugging info and receiving the following error: `invalid argument supplied for foreach()` – Michelle M. May 08 '17 at 06:17
  • You checked if ['download'] is set but not ['checked']. Inspect what you're receiving with this `printf( '
    %s
    ', print_r($_POST,true) );`
    – brasofilo May 08 '17 at 11:42

0 Answers0