Laravel - link to a file saved in storage folder

China☆狼群 提交于 2019-11-28 05:36:06

问题


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


回答1:


UPDATE:

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

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.

Hope this helps!




回答2:


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

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



回答3:


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:

  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.

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.



来源:https://stackoverflow.com/questions/40936332/laravel-link-to-a-file-saved-in-storage-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!