简体   繁体   中英

Download files from storage in Laravel

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

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

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

filesystems.php

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

DownloadController.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']);
                }
            }
}

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

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM