从 Laravel 中的存储下载文件

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

Download files from storage in Laravel

phplaravellaravel-5

提问by Sarath TS

I can't download files from storage. Firebug console shows

我无法从存储下载文件。Firebug 控制台显示

FileNotFoundException in File.php line 41: The file "C:\xampp\htdocs\ct\storage/30e9734e1f06bf751da5b3289994fb3f/34.pdf" does not exist

File.php 第 41 行中的 FileNotFoundException:文件“C:\xampp\htdocs\ct\storage/30e9734e1f06bf751da5b3289994fb3f/34.pdf”不存在

But files are existing in C:\xampp\htdocs\ct\storage\userdata\30e9734e1f06bf751da5b3289994fb3ffolder. I think the file path I mentioned in code is not correct. Please help me to solve this issue.

但是文件存在于C:\xampp\htdocs\ct\storage\userdata\30e9734e1f06bf751da5b3289994fb3f文件夹中。我认为我在代码中提到的文件路径不正确。请帮我解决这个问题。

filesystems.php

文件系统.php

'root'   => storage_path('userdata'),

DownloadController.php

下载控制器.php

    public function show(TagambitionRequest $request)
        {
            $details             = User::select('id', 'created_at')->findOrFail(Auth::user()->id);
            $encrypt             = md5($details->id.$details->created_at);
            $directories         = Storage::files($encrypt);                                             // Listout Files
            foreach($directories as $values)
            {
                $split_folder_file = explode('/', $values);           //60e4dda43c442fe610bdbd4a0e5c3a12/19.pdf
                $splitted_file     = end($split_folder_file);         //19.pdf
                $explode_filename  = explode('.', $splitted_file);    //explode(19.pdf)
                $explode_name      = $explode_filename[0];            //19
                $file_extension    = $explode_filename[1];            //pdf
                if ($file_extension == 'pdf') {
                    $path  = storage_path().'/'.$encrypt.'/'.$splitted_file;
                    return response()->download($path, $splitted_file, ['Content-Type' => 'application/pdf']);
                }
            }
}

采纳答案by l3ehnam

You've missed the userdatafolder in your path so you need to change

您错过了userdata路径中的文件夹,因此您需要更改

$path = storage_path().'/'.$encrypt.'/'.$splitted_file;

$path = storage_path().'/'.$encrypt.'/'.$splitted_file;

to

$path = storage_path().'/userdata/'.$encrypt.'/'.$splitted_file;

$path = storage_path().'/userdata/'.$encrypt.'/'.$splitted_file;