dsfdf854456 2017-05-04 18:00
浏览 120
已采纳

Laravel存储文件的公共URL

I want to retrieve public url for all the files stored using

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

So, this is the issue I'm using

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

but it provides this output from laravel storage directory

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

how can I get the public link for the above

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

**Edit **

Sending last modified data of files to view

$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'));

Is this correct

  • 写回答

3条回答 默认 最新

  • dongzhe3171 2017-05-04 18:32
    关注

    What about Storage::url? It works even for local storage.

    You can find more here: https://laravel.com/docs/5.4/filesystem#file-urls

    If you want to return all files' urls from the directory, you can do something like this:

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

    Don't forget to inject the \Illuminate\Filesystem\FilesystemManager instead of the Storage facade if you are looking for a not-facade way.

    Edit:

    There are 2 (or more) ways how you can deal with modified date:

    Pass files to the view.

    |ou can pass your Storage::files($directory) directly to the view and then in your template use following:

    // 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 an array:

    return collect(Storage::files($directory))->map(function($file) {
         return [
             'file' => Storage::url($file),
             'modified' => $file->lastModified // or something like this
         ]
    })->toArray()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入