【问题标题】:Laravel Public url for storage filesLaravel 存储文件的公共 url
【发布时间】:2017-10-03 01:00:08
【问题描述】:

我想检索所有使用

存储的文件的公共 URL

storage::putFile('public/spares');

所以,这就是我正在使用的问题

storage::files('public/spares');

但它提供了来自 laravel 存储目录的输出

public/spares/image1.jpg
public/spares/image2.jpg
public/spares/image3.jpg

我怎样才能得到上面的公共链接

http://localhost/laravel/public/storage/spares/image1.jpg
http://localhost/laravel/public/storage/spares/image2.jpg
http://localhost/laravel/public/storage/spares/image3.jpg

**编辑**

发送文件的最后修改数据以查看

$docs = File::files('storage/document');
$lastmodified = [];
foreach ($docs as $key => $value) {
   $docs[$key] = asset($value);
   $lastmodified[$key] = File::lastmodified($value);
}
return view('stock.document',compact('docs','lastmodified'));

这样说对吗

【问题讨论】:

  • 你使用的是什么版本的 Laravel?
  • 我使用的是 laravel 5.4

标签: php laravel file-upload laravel-5.4 php-7


【解决方案1】:

好吧,我找到了解决方案,而不是搜索存储

$docs = Storage:files('public/spares');

我们可以搜索符号链接

$docs = File:files('storage/spares');

然后通过asset() 函数运行它以获取公共URL。有更好的解决方案吗?

【讨论】:

    【解决方案2】:

    Storage::url 呢?它甚至适用于本地存储。

    您可以在这里找到更多信息:https://laravel.com/docs/5.4/filesystem#file-urls

    如果你想从目录中返回所有文件的 url,你可以这样做:

    return collect(Storage::files($directory))->map(function($file) {
        return Storage::url($file);
    })
    

    如果您正在寻找非门面方式,请不要忘记注入\Illuminate\Filesystem\FilesystemManager 而不是Storage 门面。

    编辑:

    有两种(或更多)方法可以处理修改日期:

    将文件传递给视图。

    |您可以将Storage::files($directory) 直接传递给视图,然后在您的模板中使用以下内容:

    // controller:
    
    return view('view', ['files' => Storage::files($directory)]);
    
    // template:
    
    @foreach($files as $file)
       {{ Storage::url($file) }} - {{ $file->lastModified }} // I'm not sure about lastModified property, but you get the point
    @endforeach
    

    返回一个数组:

    return collect(Storage::files($directory))->map(function($file) {
         return [
             'file' => Storage::url($file),
             'modified' => $file->lastModified // or something like this
         ]
    })->toArray()
    

    【讨论】:

    • 它适用于一个文件没有在文件夹中获取文件数组。
    • @DanielEuchar 那么为什么不遍历文件夹(Storage::files($directory) - laravel.com/docs/5.4/filesystem#directories)然后对每个文件使用Storage::url 并作为集合返回?我更新了我的答案。
    • 从来不知道会尝试更新。谢谢
    • 当然,我们会解决的! :) 让我知道它是否有效
    • 太棒了,它的工作原理,以及现在还附上最后修改的数据,我是创建一个单独的变量还是有可能使用集合?
    【解决方案3】:

    首先,您必须创建从public/storage 目录到storage/app/public 目录的符号链接,以便您可以访问这些文件。你可以这样做:

    php artisan storage:link
    

    那么您可以使用以下方式存储您的文档:

    Storage::putFile('spares', $file);
    

    并将它们作为资产访问:

    asset('storage/spares/filename.ext');
    

    查看documentation on the public disk

    【讨论】:

    • 我想要实现的是获取目录中的文件列表并将其显示在我的视图中。您提供的解决方案适用于单一资产,而不适用于获取完整列表。
    【解决方案4】:

    我知道这个问题已经很老了,但我将这个答案提供给在 laravel 5.5+ 中仍然存在这个问题的任何人

    要解决此问题,请通过添加 'url' 键来更新文件系统的配置,如下所示:

    'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],
    

    希望对你有帮助:)

    【讨论】:

      猜你喜欢
      • 2021-09-08
      • 1970-01-01
      • 2016-08-29
      • 2020-01-21
      • 2016-06-05
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2017-10-11
      相关资源
      最近更新 更多