Download laravel zip file

1

I have this code that generates a zip with the images I keep in the download directory, these images are previously selected ...

Example for the first shipment:

I select image1, image2, image3 and image4, therefore in my download directory there will only be these 4 images and of course the zip will be created with those images.

Code for the first sent

  $checked = $request->input('catalog');

     foreach($checked as $check){
        $path = File::where('name',$check)->select('real_path')->first();
        $img = \Image::make($path->real_path);
        $img->save(public_path('download'). '/'. $check);
           }

        $files = \File::files('download');
        \Zipper::make(public_path('download/download.zip'))->add($files);
        return response()->download(public_path('download/download.zip'));

Example for the second sent:

This time only select image1, image2, then I need the zip to only contain those 2 images, so it occurred to me to do this:

Delete the contents of the download directory if the download.zip exists:

 if (\File::exists('download/download.zip')) {
     $directory_cuts   = public_path('download');
     $success   = \File::cleanDirectory($directory_cuts);
    }

    foreach($checked as $check){
       $path = File::where('name',$check)->select('real_path')->first();
       $img = \Image::make($path->real_path);
       $img->save(public_path('download'). '/'. $check);
    }

    $files = \File::files('download');
    \Zipper::make(public_path('download/download.zip'))->add($files);
     return response()->download(public_path('download/download.zip'));

The code works in the following points:

  • Remove the contents of the download directory.
  • Generates the new selected images.
  • Generate the zip file, with the generated images.

But it does not work in the most important one, which is to download the ZIP.

I get the following error:

The file "C:\wamp\www\Petro\public\download\download.zip" does not exist

Is there something wrong with my code?

And if there is another way to download multiple files, I would also like to know.

    
asked by Elio 13.08.2016 в 04:31
source

1 answer

1

remember that accessing a file on the hard drive is bad enough.

The problem is that you want to bring it from a private directory of the system (ie wamp / www),

which is entered from c: / wamp / www / ... etc.

The problem is that it generates it with the inverse diagonals c: \ wamp \ www ... so it does not interpret the system.

So 1 of 2, or you work on a linux, or you change the path of my friend.

    
answered by 14.08.2016 в 18:51