Laravel - link 保存到存储文件夹中的文件

Laravel - link to a file saved in storage folder

当我们做这样的事情时:

Storage::disk('local')->put('file.txt', 'Contents');

如何在视图中为该文件创建 link?像这样:

<a href="path_to_file.txt">Download File</a>

文档中有很多内容,但甚至没有一个示例如何为该文件创建 link

更新:

根据 laravel docs,您可以像这样获取文件的 URL:

use Illuminate\Support\Facades\Storage;

$url = Storage::url('file1.jpg');

Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory.

希望对您有所帮助!

在您的终端上尝试此命令:php artisan storage:link,然后 laravel 存储变为 public 访问。

<a href="{{url('/')}}/{{ Storage::disk('local')->url('file.txt')}}">Download File</a>

场景--(从一个新的 Laravel 5.6 项目安装开始工作以供参考): 现有的默认路径为 /public 和 /storage/app/public 并且您希望将 logo.png 图像物理存储在 /storage 文件夹中并将其呈现在欢迎页面上,该过程类似于这个:

  1. 在您的终端中,导航至您的 Laravel 项目文件夹。
  2. 运行命令php artisan storage:link
  3. 现在在您的 /storage/app/public 文件夹中创建文件夹 /images,它为您提供 /storage/app/public/images。
  4. 查看您的 /public 文件夹,您将看到(快捷方式)子文件夹 /storage/images
  5. 将名为 logo.png 的测试图像复制到 /storage/app/public/images 文件夹中。
  6. 在您项目的 welcome.blade.php 下 /resources/views 粘贴 以下代码覆盖现有代码:

    <div class="content">
        <div class="title m-b-md">
            Laravel
            <div>
                <a href="/">
                  <img src="{{url('/storage/images/logo.png')}}" alt="Logo Image"/>
                </a>
            </div>
        </div>
    </div>
    
  7. 保存并在浏览器中打开您的项目,您应该会看到徽标图像。

说了这么多,后面你需要一个pdf文件夹来存放上传的pdf 文件。很简单,只需在 /storage/app/public 中创建一个名为 /pdf 的新文件夹,然后 快捷方式将自动出现在 /public 文件夹中。这是一次性 "for all" 解决方案。