laravel 5.3 upload file

安稳与你 提交于 2019-12-13 09:40:05

问题


I'm using laravel 5.3, and i want to upload a file to a directory. i've been following laravel documentation for uploading file, like this code :

public function store(Request $request)
{
    $path = $request->attachment->store('attachment');
}

but it get an error

Call to a member function store() on string

previously on laravel 5.2, i've been use this code for uploading file and it's work

if ($request->hasFile('attachment')) {
    $destination = 'upload/attachment';
    $file = $request->file('attachment');
    $file->move($destination, $file->getClientOriginalName());
}

$filelocation = $destination. "/" .$file->getClientOriginalName();

but in laravel 5.3 it's doesn't work, and i get an error

Undefined variable: destination

can you help what wrong with my code ?


回答1:


public function store(Request $request)
{
    $path = $request->attachment->store('attachment');
}

I don't think $request->attachment does what you are trying to do. Change that line to:

$request->file('attachment')->store('/your/destination/path')

if($request->hasFile('attachment')){
    $destination = 'upload/attachment';
    $file = $request->file('attachment');
    $file->move($destination, $file->getClientOriginalName());
}

$filelocation = $destination. "/" .$file->getClientOriginalName();

When $request->hasFile('attachment') is false, $destination will not get declared. So when it reaches the $filelocation line you get an undefined variable.




回答2:


In Larave 5.3 you can use simple upload file like this:

$request->file('file')->storePublicly($folderSrc);

This save you file to storage/app/public/you-folder-src



来源:https://stackoverflow.com/questions/40040839/laravel-5-3-upload-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!