简体   繁体   中英

laravel 5.3 upload file

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 ?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM