laravel 5.4 upload image
Try this code. This will solve your problem.
public function fileUpload(Request $request) { $this->validate($request, [ 'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', ]); if ($request->hasFile('input_img')) { $image = $request->file('input_img'); $name = time().'.'.$image->getClientOriginalExtension(); $destinationPath = public_path('/images'); $image->move($destinationPath, $name); $this->save(); return back()->with('success','Image Upload successfully'); }}
You can use it by easy way, through store
method in your controller
like the below
First, we must create a form with file input to let us upload our file.
{{Form::open(['route' => 'user.store', 'files' => true])}}{{Form::label('user_photo', 'User Photo',['class' => 'control-label'])}}{{Form::file('user_photo')}}{{Form::submit('Save', ['class' => 'btn btn-success'])}}{{Form::close()}}
Here is how we can handle file in our controller.
<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;use App\Http\Controllers\Controller;class UserController extends Controller{ public function store(Request $request) { // get current time and append the upload file extension to it, // then put that name to $photoName variable. $photoName = time().'.'.$request->user_photo->getClientOriginalExtension(); /* talk the select file and move it public directory and make avatars folder if doesn't exsit then give it that unique name. */ $request->user_photo->move(public_path('avatars'), $photoName); }}
That’s it. Now you can save the $photoName
to the database as a user_photo
field value. You can use asset(‘avatars’)
function in your view and access the photos.
A good logic for your application could be something like:
public function uploadGalery(Request $request){ $this->validate($request, [ 'file' => 'required|image|mimes:jpeg,png,jpg,bmp,gif,svg|max:2048', ]); if ($request->hasFile('file')) { $image = $request->file('file'); $name = time().'.'.$image->getClientOriginalExtension(); $destinationPath = public_path('/storage/galeryImages/'); $image->move($destinationPath, $name); $this->save(); return back()->with('success','Image Upload successfully'); } }