【问题标题】:Retrieving multiple files(images) from storage in Laravel从 Laravel 的存储中检索多个文件(图像)
【发布时间】:2020-08-11 19:45:57
【问题描述】:

我正在尝试检索上传到存储/应用程序/客户端中本地磁盘的文件。我不想公开。我看到人们检索多个文件的所有方法都是将它们放在公用文件夹中,但我想保留给登录的用户。

我的上传文件控制器(formdata):

PicturesController.php

public function store(Request $request)
{
    $images = $request->file();
    $client = $request->get("ClientID");
    $treatment = $request->get("TreatmentID");

    $path = 'clients\\' . $client . '\\' . $treatment . '\\';


    foreach ($images as $image) {

        //StorePicture
        $randomName = Str::random(8);
        $date = date("_d-m-Y_H-i-s.", time());
        $extension = $image->extension();
        $filename = $randomName.$date.$extension;
        $image->storeAs($path, $filename);

        //Save to database
        $picture = new Picture();
        $picture->picPath = $path.$filename;
        $picture->Treatment()->associate($treatment);

        $picture->save();
    }

}

这会毫无问题地存储文件。

我到处寻找,但没有找到任何解决方案来检索图片并将它们传递给 vue.js 前端。 有没有人有这个完成或想法的链接?

到目前为止:

public function show($id)
{
    //Collection of Picture (paths...)
    $picturesCollection= Picture::where('fkTreatment', $id)->orderBy('created_at', 'desc')->get();

    $images = array();

    foreach ($picturesCollection as $pic) {
        //Store images in an array to return it to ModalDisplay.vue
        $images[] = ?;
    }

    return $images;
}

感谢阅读。

【问题讨论】:

    标签: php laravel vue.js axios


    【解决方案1】:

    TL:DR

    picPath 属性应该包含实际的文件路径

    我正在与您分享一个我用来管理我的文件的 sn-p

        $auth_id = 0;
        $storage_details = [];
    
        if (auth()->check()) {
            $auth_id = auth()->user()->id;
        }
    
        $file_name = $auth_id . '_' . date('d-m-Y_H-i-s') . '.' . $file->getClientOriginalExtension();
    
        $store = $this->getStorage()->putFileAs('records', $file, $file_name);
        if (!$store) {
            return $storage_details;
    
        } else {
    
            $storage_details['file_path'] = $this->getStorage()->getDriver()->getAdapter()->getPathPrefix() . 'records' . DIRECTORY_SEPARATOR . $file_name;
            $storage_details['file_name'] = $file_name;
            $storage_details['driver'] = $this->getStorage()->getDriver();
            $storage_details['disk'] = $this->getDisk();
            $storage_details['relative_path'] = 'records' . DIRECTORY_SEPARATOR . $file_name;
    
            return $storage_details;
        }
    

    这就是我如何将它们保存到数据库

        $task = new self();
        $task->id = $task_id;
        $task->user_id = auth()->user()->id;
        $task->type = 'process_records';
    
        $meta = [];
        if (array_key_exists('file_path', $storage_details)) {
            $meta['file_path'] = $storage_details['file_path'];
        }
        if (array_key_exists('file_name', $storage_details)) {
            $meta['file_name'] = $storage_details['file_name'];
        }
        if (array_key_exists('driver', $storage_details)) {
            $meta['driver'] = $storage_details['driver'];
        }
        if (array_key_exists('disk', $storage_details)) {
            $meta['disk'] = $storage_details['disk'];
        }
        if (array_key_exists('relative_path', $storage_details)) {
            $meta['relative_path'] = $storage_details['relative_path'];
        }
    
        $task->meta = $meta;
        $task->save();
    

    现在您只需要迭代相对路径,如果将来您将驱动程序更改为 AWS 或其他您可以轻松实现逻辑的东西

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-07
      • 1970-01-01
      • 2020-10-23
      • 1970-01-01
      相关资源
      最近更新 更多