从 Laravel 中的存储服务文件(符号链接自定义存储路径)

Serve files from storage in Laravel (symlink custom storage path)

我想为特定领域(迷你 youtube 之类的东西)创建一个视频共享网站。

我已成功创建上传功能,文件存储在storage/public/videos

然后我使用 php artisan storage:link

创建了一个符号链接

当我尝试使用 echo asset('storage/videos/file.mp4'); 访问文件时,如 https://laravel.com/docs/5.6/filesystem

所述

我的 html 在视图中看起来像

<video controls>

<source src="{{ asset('storage/videos/file.mp4') }}" type="video/mp4">

</video>

问题:我做错了什么或遗漏了什么?

所以从 documentation:

The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public. This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.

如您所知,当您执行 php artisan storage:link 时,它会执行以下操作:

ln -s storage/app/public public/storage

但是您将您的文件保存在 storage/public/videos 中,文档无法访问(链接)这些文件。

2个解决方案:

1) 您必须手动执行(手动创建符号链接):

ln -s storage/public/videos public/storage/videos

并在代码中使用它:asset('storage/videos/file.mp4')

2) 更改文件存储方式为按约定存储文件:storage/app/public/videos