title: PHP Temp Directory issue
body: <p>I am new to this temp directory thing and managing files on the backend with PHP so any help would be great. So the gist here is that I click to download either one specific file or all of the files. The trick is that depending on a particular condition, the data is coming back from the API as a stream and the other is coming back as the content (in this case html). In my eyes this logic should work. I have the message coming back as either true or false to determine whether it is a stream or if it is the content of the file. But something about the two conditions is breaking. I've managed to get all sorts of cases working. Individually they download fine but then the bulk download breaks or the other way around if I move the code around. Any suggestions?</p>
  
  <pre><code>public function downloadTranslations(Request $request, $id)
  {
      $target_locales = $request-&gt;input("target_locale");
      $has_source = $request-&gt;input("source");
      $client = new API(Auth::user()-&gt;access_token, ZKEnvHelper::env('API_URL', 'https://myaccount.site.com'));
      $document = Document::find($id);
      $job_document = JobDocument::where('document_id', $id)-&gt;first();
      $job = Job::find($job_document-&gt;job_id);
  
      $file = tempnam('tmp', 'zip');
      $zip = new ZipArchive();
      $zip-&gt;open($file, ZipArchive::OVERWRITE);
      $name_and_extension = explode('.', $document-&gt;name);
  
      if($target_locales == null){
          $target_locales = [];
          foreach ($job-&gt;target_languages as $target_language) {
              $target_locales[] = $target_language['locale'];
          }
      }
      foreach($target_locales as $target_locale){
          $translation = $client-&gt;downloadDocument($document-&gt;document_id, $target_locale);
          $stream = Stream::factory($translation-&gt;get('body'));
              $filename = $name_and_extension[0] . ' (' . $target_locale . ').' . $name_and_extension[1];
              if($translation-&gt;get('message') == 'true') {
  
                  $tmpfile = tempnam(sys_get_temp_dir(), 'lingo');
  
                  $handle = fopen($tmpfile, 'w');
                  fwrite($handle, $stream-&gt;getContents());
                  $zip-&gt;addFile($tmpfile, $filename);
  
                  fclose($handle);
              }
              else if($translation-&gt;get('message') == 'false'){
                  $zip-&gt;addFromString($filename, $translation-&gt;get('body'));
              };
          }
          $translation = $client-&gt;downloadDocument($document-&gt;document_id, null, null);
  
          $filename = $name_and_extension[0].  ' (Source).'.$name_and_extension[1];
          $zip-&gt;addFromString($filename, $translation-&gt;get('body'));
          $zip-&gt;close();
  
      return response()-&gt;download($file, $name_and_extension[0].'.zip')-&gt;deleteFileAfterSend(true);
  </code></pre>