php 用于存储文件的 Laravel 公共 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43789804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:54:40  来源:igfitidea点击:

Laravel Public url for storage files

phplaravelfile-uploadlaravel-5.4php-7

提问by Daniel Euchar

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

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

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

storage::putFile('公共/备件');

So, this is the issue I'm using

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

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

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

but it provides this output from laravel storage directory

但它从 laravel 存储目录提供此输出

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

这样对吗

回答by Eric Tucker

First you have to create the symbolic link from your public/storagedirectory to your storage/app/publicdirectory so you can access the files. You can do that with:

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

php artisan storage:link

So then you can store your documents with:

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

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

And access them as an asset with:

并将它们作为资产访问:

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

Check out the documentation on the public disk

查看公盘上的文档

回答by Jakub Kratina

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

怎么样Storage::url?它甚至适用于本地存储。

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

您可以在此处找到更多信息: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:

如果要从目录中返回所有文件的 url,可以执行以下操作:

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

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

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

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:

|您可以将您的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 an array:

返回一个数组:

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

回答by Mazino S Ukah

I know this question is quite old, but i am putting this answer for anyone still having this issue from laravel 5.5+

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

To solve the issue update your filesystem's configuration by adding the 'url' key like below:

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

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

Hope it helps :)

希望能帮助到你 :)

回答by Daniel Euchar

Well I figured out the solution, instead of searching the storage

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

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

we can search the symbolic link

我们可以搜索符号链接

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

then run it through the asset()function to get the public URL. Is there a better solution?

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