enctype="multipart/form-data"
must be added to the enctype="multipart/form-data"
to allow file uploads.
<form method="post" action="" enctype="multipart/form-data"> <input type="file" name="image"> <button type="submit">Отп</button> </form>
Next, create a controller:
public function fileUpload(Request $request){ if($request->isMethod('post')){ if($request->hasFile('image')) { $file = $request->file('image'); $file->move(public_path() . '/path','filename.img'); } } }
Where
- $ request-> hasFile ('image') `- check for the existence of a file, specify the field identifier as an argument
$file = $request->file('image')
- write file object to variable$file-move(public_path() . '/path','filename.img')
is a method for moving a file to the directory we need, the 1st argument specifies the directory in which to save the file, the 2nd indicates the new file namepublic_path()
- will return us the full path to the public directory
$set
variable? The documentation does not explicitly use this check:$request->image!= ''
. - VenZell<form method="post" enctype="multipart/form-data">
- apelsinka223