问题
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