Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.4 Storage : downloading files. File does not exist, but clearly it does

Tags:

laravel

I have been going round and round with this. I have uploads working with the follow:

public function store(Tool $tool)
{

     If(Input::hasFile('file')){
        $file = Input::file('file');
        $name = $file->getClientOriginalName();
        $path=Storage::put('public',$file);         //Storage::disk('local')->put($name,$file,'public');

        $file = new File;
        $file->tool_id = $tool->id;
        $file->file_name = $name;
        $file->path_to_file = $path;
        $file->name_on_disk = basename($path);
        $file->user_name = \Auth::user()->name;
        $file->save();

        return back();
    }

however when I try to download with:

public function show($filename)
    {   

        $url = Storage::disk('public')->url($filename);

        ///$file = Storage::disk('public')->get($filename);
        return response()->download($url);
    }

I get the FileNotFound exception from laravel However, if I use this instead:

$file = Storage::disk('public')->get($filename);
return response()->download($file);

I get

FileNotFoundException in File.php line 37: The file "use calib;

insert into notes(tool_id,user_id,note,created_at,updated_at) VALUES(1,1,'windows server 2008 sucks',now(),now());" does not exist

which is the actual content of the file...

It can obviously find the file. but why wont it download?

like image 772
newGuy Avatar asked Feb 08 '17 14:02

newGuy


1 Answers

Try this:

return response()->download(storage_path("app/public/{$filename}"));
like image 77
Paras Avatar answered Oct 05 '22 05:10

Paras