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 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题