newGuy
newGuy

Reputation: 354

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

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?

Upvotes: 15

Views: 50889

Answers (3)

Bruce Tong
Bruce Tong

Reputation: 1431

If any one still could not find their file even though the file clearly exists then try

return response()->file(storage_path('/app/' . $filename, $headers));

It could be due to a missing directory separator or it isn't stored inside the public folder.

Upvotes: 4

rap-2-h
rap-2-h

Reputation: 32068

Replace:

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

With:

return response()->download(storage_path('app/public/' . $filename));

response()->download() takes a path to a file, not a file content. More information here: https://laravel.com/docs/5.4/responses#file-downloads

Upvotes: 7

Paras
Paras

Reputation: 9465

Try this:

return response()->download(storage_path("app/public/{$filename}"));

Upvotes: 44

Related Questions