简体   繁体   English

Laravel - S3 多个文件 zip 并下载

[英]Laravel - S3 multiple file zip and download

Is there any way to loop or perform some foreach action inside return in Laravel?有什么方法可以在 Laravel 中的 return 中循环或执行一些 foreach 操作吗? I have this package https://github.com/stechstudio/laravel-zipstream for zip downloading from s3.我有这个 package https://github.com/stechstudio/laravel-zipstream zip 从 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:这是从 $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?您可能需要更改我使用的field names ,因为它们可能不准确,但是,像下面这样的东西呢?

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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