title: Downloading a zip file from a folder on my web server
body: <p>I have an application that allows users to download mulitple files from a S3 bucket through an EC2 instance in one zip folder.</p>
  
  <p>The process works fine, however, I would like the system to create a folder for each user when they want to download files, and then the files are downloaded to this folder and then put in a zip folder, within the user's folder, and then this zip folder is downloaded.</p>
  
  <p>The files are downloading to this folder fine, and they are also zipping correctly, but my problem is that it is not downloading the zip file from the user's folder, and the download is showing as an empty zip file.</p>
  
  <p>Below is my 'download all' code:</p>
  
  <pre><code>mkdir($userId);
  $allName = $vshort . "v" . $number . "_All.zip";
  $allName = str_replace(" ", "_", $allName);
  if (isset($_POST['downloadAll']))
  {
     $zip = new ZipArchive();
     $zip-&gt;open("{$userId}/{$allName}", ZipArchive::CREATE);
     $s3-&gt;registerStreamWrapper();
     foreach ($items as $item)
     {
          $fid = $item-&gt;item_file_id;
          $file = ItemFile::locateId($fid);
          $a = $file[0]-&gt;item_file_type;
          $b = $file[0]-&gt;item_file_app;
          $c = $file[0]-&gt;item_file_name;
          $fileName = $a . "/" . $b . "/" . $c;
          $download = $s3-&gt;getCommand('GetObject', [
               'Bucket' =&gt; AWS_BUCKET,
               'Key' =&gt; $fileName
          ]);
          $streamFile = $c;
          $user = User::locate($_SESSION['email']);
          $uid = $user-&gt;user_id;
          $cid = $user-&gt;user_company_id;
          $history = new History();
          $history-&gt;insert($fid, $uid, $cid);
          $req = $s3-&gt;createPresignedRequest($download, '+1 minutes');
          $link = (string)$req-&gt;getUri();
          $stream = file_get_contents($link);
          header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
          header("Cache-Control: public"); // needed for internet explorer
          header("Content-Type: application/'$a'");
          header("Content-Disposition: attachment; filename='$c'");
          file_put_contents($streamFile, $stream);
          $zip-&gt;addFile($streamFile);
          $downArray[] = $streamFile;
     }
     $zip-&gt;close();
     $dLink = "https://mywebsite/" . $userId . "/" . $allName;
     $size = filesize($allName);
     header("Content-type: application/zip");
     header("Content-Disposition: attachment; filename=$allName");
     header("Content-length: " . $size);
     header("Expires: 0");
     ob_end_clean();
     stream_context_set_default([
         'ssl' =&gt; [
               'verify_peer' =&gt; false,
                'verify_peer_name' =&gt; false,
          ]
     ]);
     readfile($dLink);
     unlink($allName);
     foreach ($downArray as $down)
     {
         unlink($down);
     }
  }
  </code></pre>
  
  <p>I have a feeling it is something to do with my headers but I'm not sure, any help will be greatly appreciated.</p>
  
  <p>NOTE: I will be deleting the folder once the user has exited the page.</p>
  
  <p>Below is what I am seeing on the web server
  <a href="https://i.stack.imgur.com/7Pt60.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7Pt60.png" alt="enter image description here"></a></p>
  
  <p>The folder '6' is the user id for the user in question</p>