简体   繁体   中英

Laravel - S3 multiple file zip and download

Is there any way to loop or perform some foreach action inside return in Laravel? I have this package https://github.com/stechstudio/laravel-zipstream for zip downloading from s3. All works for single file and for multiple, but only when each files are provided in separate line.

use Zip; 
public function z4()
    {
    return Zip::create("package.zip")
                ->add("s3://testbucket/images/26/17/full/BS288.jpg", "BS288.jpg")
                ->add("s3://testbucket/images/26/17/full/BS289.jpg", "BS289.jpg")
                ->add("s3://testbucket/images/26/17/full/BS290.jpg", "BS290.jpg")
    }

but how to make this with data from database

$photos = DB::table('gallery_image')
                ->where('folder_id','=',$id)
                ->select('original_name')->get();

This is array returned from $photos:

  Illuminate\Support\Collection {#1017 ▼
        #items: array:131 [▼
          0 => {#1025 ▼
            +"original_name": "BS288.jpg"
          }
          1 => {#1022 ▼
            +"original_name": "BS289.jpg"
          }
          2 => {#1026 ▼
            +"original_name": "BS290.jpg"
          }

this code will return always one file

foreach($photos as $image) {
     
            return Zip::create("package.zip", [
                "s3://asystentfotografa/images/26/17/full/".$image->original_name, 
              ]);
 }

You might need to change the field names I have used as they might not be accurate, however, what about something like the following?

public function z4()
{
    $photos = DB::table('gallery_image')
                ->where('folder_id','=',$id)
                ->select('original_name')->get();

    $zip = Zip::create('package.zip');

    foreach ($photos as $photo) {
        $zip->add('s3://testbucket/images/' . $photo->gallery_id . '/' . $photo->folder_id . '/full/' . $photo->original_name);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM