Laravel 存储文件的公共 url

新手上路,请多包涵

我想检索使用存储的所有文件的公共 url

存储::putFile(‘公共/备件’);

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

存储::文件(“公共/备件”);

但它提供了来自 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'));

这个对吗

原文由 Daniel Euchar 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 543
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()

原文由 Jakub Kratina 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

 php artisan storage:link

因此,您可以使用以下方式存储您的文档:

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

并通过以下方式将它们作为资产访问:

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

查看 公共磁盘上的文档

原文由 Eric Tucker 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题