title: How to download multiple image from database in laravel 5.4
body: <p>In my <strong>laravel 5.4</strong> app, I want to download multiple images of the selected product from the database it is creating multiple zip file in <code>public_path('uploads/download-images');</code> but not downloading the file how can i download this.</p>
  
  <p>what is the best possible way to download this? </p>
  
  <p>what am i doing wrong ? can anybody help? thank in advance.</p>
  
  <p>Following is my code.</p>
  
  <pre><code>public function downloads_all(Request $request) 
   {
      $public_dir = '';
      $$zipFileName = '';
      $zip = '';
      $product_images =  DB::table('product_images')
                  -&gt;where('deleted_at', null)
                  -&gt;whereIn('product_id',  explode(',', $request-&gt;ids))
                  -&gt;get();
      if ($product_images) 
      {
          foreach ($product_images as $value) 
          {
              $zipFileName = 'product_' . $value-&gt;product_id . '_images.zip';
              $zip = new ZipArchive;
              $public_dir = public_path('uploads/download-images');
              if (!is_dir($public_dir)) 
              {
                  mkdir($public_dir, 0777, true);
              }
              if ($zip-&gt;open($public_dir . '/' . $zipFileName, ZipArchive::CREATE) === TRUE) 
              {
                  foreach ($product_images as $image) 
                  {
                      $directory_path = base_path() . '/public/uploads/products/' . $image-&gt;product_id . '/images/';
                      $image_path = $image-&gt;image_path;
                      $file_name = basename($image_path);
                      $zip-&gt;addFile($directory_path . $file_name, $file_name);
                  }
                  $zip-&gt;close();
              }
          }
  
          $files = File::files(public_path('uploads/download-images'));
          $filecount = count($files);
          $filetopath = array();
          for ($i=0; $i &lt; $filecount; $i++) 
          { 
              $filetopath = $public_dir . '/' . $zipFileName;
              $headers = array(
                  'Content-Type' =&gt; 'application/octet-stream',
                  'Expires' =&gt; 0,
                  'Content-Transfer-Encoding' =&gt; 'binary',
                  'Content-Length' =&gt; filesize($filetopath),
                  'Cache-Control' =&gt; 'private, no-transform, no-store, must-revalidate',
                  'Content-Disposition' =&gt; 'attachment; filename="' . $zipFileName . '"',
              );
  
              if (file_exists($filetopath)) 
              {
                  return response()-&gt;download($filetopath, $zipFileName, $headers)-&gt;deleteFileAfterSend(true);
              } 
              else 
              {
                  return ['status'=&gt;'zip file does not exist'];
              }
          }
      }
      else
      {
          return back();
      }
  }
  </code></pre>
  
  <p>this is the js</p>
  
  <pre><code>$(document).ready(function () 
  {
      $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
      });
  
      $('#download-master').on('click', function(e) 
      {
          if($(this).is(':checked',true))  
          {
              $(".multiple_download").prop('checked', true);  
          } else {  
              $(".multiple_download").prop('checked',false);  
          }  
      });
  
      $('.download-all').on('click', function(e) {
  
          var allVals = [];  
          $(".multiple_download:checked").each(function() {  
              allVals.push($(this).attr('data-id'));
          });  
  
          if(allVals.length &lt;=0)  
          {  
              alert("Please select row to download.");  
          }  
          else 
          {  
              var join_selected_values = allVals.join(",");
              $.ajax({
                  url: $(this).data('url'),
                  type: 'POST',
                  data:'_token = &lt;?php echo csrf_token() ?&gt;',
                  data: 'ids='+join_selected_values,
                  success: function (data) 
                  {
                      alert('Seccussfully Downloaded');
                  },
                  error: function (data) {
                      alert(data.responseText);
                  }
              }); 
          }  
      });
  });
  </code></pre>