Laravel - 链接到保存在存储文件夹中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40936332/
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-09-14 14:51:42  来源:igfitidea点击:

Laravel - link to a file saved in storage folder

laravellaravel-5.3

提问by lewis4u

When we do something like this:

当我们做这样的事情时:

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

How do you make a link to that file in a view? Something like this:

如何在视图中创建指向该文件的链接?像这样的东西:

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

there are so many things in documentation and yet not even one example how to create a link to that file

文档中有很多东西,但甚至没有一个示例如何创建指向该文件的链接

回答by susilogn

Try this command on your terminal : php artisan storage:link, then laravel storage become public access.

在你的终端上试试这个命令:php artisan storage:link,然后 Laravel 存储成为公共访问。

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

回答by Saumya Rastogi

UPDATE:

更新

According to laravel docs, You can get the URL to the file like this:

根据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/publicdirectory. Furthermore, you should create a symbolic link at public/storagewhich points to the storage/app/publicdirectory.

请记住,如果您使用的是本地驱动程序,则所有应可公开访问的文件都应放在该storage/app/public目录中。此外,您应该创建一个public/storage指向storage/app/public目录的符号链接 。

Hope this helps!

希望这可以帮助!

回答by John

Scenario--(working from a fresh Laravel 5.6 project install for reference): With the existing default paths being /public and /storage/app/public and you want to physically store your logo.png image in the /storage folder and render it on your Welcome page, the process would go something like this:

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

  1. In your terminal, navigate to your Laravel project folder.
  2. Run command php artisan storage:link
  3. Now create the folder /images in your /storage/app/public folder which gives you /storage/app/public/images.
  4. Look in your /public folder and you will see the (shortcut) subfolders /storage/images
  5. Copy a test image named logo.png into the /storage/app/public/images folder.
  6. In your project's welcome.blade.php under /resources/views paste the following code over the existing code:

    <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. Save and open your project in your browser and you should see the logo image.

  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. 在浏览器中保存并打开您的项目,您应该会看到徽标图像。

Having said all of that, later on you need a pdf folder to store uploaded pdf files. Easy, just create a new folder in /storage/app/public called /pdf and automatically the shortcut will appear in the /public folder. It is a once and "for all" solution.

说了这么多,稍后您需要一个 pdf 文件夹来存储上传的 pdf 文件。简单,只需在 /storage/app/public 中创建一个名为 /pdf 的新文件夹,快捷方式将自动出现在 /public 文件夹中。这是一个“一劳永逸”的解决方案。