ziben69's avatar

Download file from database (storage)

Hello guys,

I did multiple file upload: model File:

class File extends Model
{
    protected $fillable = [
        'title',
        'description',
        'filename'
    ];
}

FileController (store function):

  public function storeFiles(Request $request)
    {

        $this->validate($request, [
            'filename' => 'required',
            'filename.*' => 'mimes:doc,pdf,docx,zip'
        ]);


        if($request->hasfile('filename'))
        {
            foreach($request->file('filename') as $file)
            {
                $name=$file->getClientOriginalName();
                $file->move(storage_path('/app/public/files/', $name));
                $data[] = $name;  
            }
        }

        $file = new File();
        $file->title = $request->input('title');
        $file->description = $request->input('description');
        $file->filename=json_encode($data);   
        $file->save();

        return redirect()->action('FilesController@flist');
    }

and how can I download file now. I have something like this:

public function show($id)
    {
        $dl = File::find($id);
        return Storage::download(storage_path('app/public/files/'), $dl->filename);      
    }

but it gives to me error:

File not found at path: C:/xampp/htdocs/tbg/storage/app/public/files

and URL: http://localhost/filess/download/8

Can someone help me? Thanks

0 likes
4 replies
realrandyallen's avatar

The file name needs to be apart of the call to download

return Storage::download(storage_path('app/public/files/') . $dl->filename); 
ziben69's avatar

Oh thats it but now I have other error:

File not found at path: C:/xampp/htdocs/tbg/storage/app/public/files/["testfile.pdf"]

in storage/app/public/files directory I have file named "php37B4.tmp"

whaaat?

ziben69's avatar

Ok I changed method move to storeAs. Now I have good file in directory named testfile.pdf, but still have problem with download:

File not found at path: C:/xampp/htdocs/tbg/storage/app/public/files/["testfile.pdf"]

probably because filename is an array, should I decode or sth?

Please or to participate in this conversation.