laravel 5.3上传文件

问题描述:

我正在使用laravel 5.3,我想将文件上传到目录.我一直在关注laravel文档来上传文件,例如以下代码:

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

但出现错误

在字符串上调用成员函数store()

Call to a member function store() on string

以前在laravel 5.2上,我一直使用此代码上传文件,并且可以正常工作

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();

但是在laravel 5.3中它不起作用,并且出现错误

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 ?

在Larave 5.3中,您可以使用简单的上传文件,如下所示:

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

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

这会将您的文件保存到storage/app/public/you-folder-src

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