mohamadAbdelhady's avatar

image upload is not working

I am trying to upload an image but laravel keep sending me error and i dont know why

my function

 function store(Request $request)
   {
       $request->validate([
           'inputimg' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
       ]);

       $imageName = time().'.'.$request->image->extension();

       $request->image->move(public_path('images'), $imageName);

       

       return back()
           ->with('success','You have successfully upload image.')
           ->with('image',$imageName);
   }

my view

                    @csrf
                    <textarea class="form-control" id="textArea" rows="10" placeholder="text here" name="textinput"></textarea>
                    <p id="error-upload" style="display:none; color: red;"></p>
                    <div class="btn-group btn-group-sm m-2" role="group" aria-label="Second group" >
                        <button type="button" class="btn btn-light " onclick="event.preventDefault(); document.getElementById('upload-img').click();"><img src="/picture_icon.png" id="home_icon"><p>Picture</p></button>
                        <input type="file"  directory  accept="image/*" style="display: none;" id="upload-img" class="form-control" name="inputimg">
                        <button type="button" class="btn btn-light"onclick="event.preventDefault(); document.getElementById('upload-video').click();"><img src="/video_icon.png" id="home_icon"><p>Video</p></button>
                        <input type="file" accept="video/*" style="display: none;" id="upload-video" class="form-control" name="inputvid">


                    </div>
                    <input type="submit" class="btn btn-outline-info " style="width: 100%;" value="Post">
                </form>

my route

Route::post('crtpost',['as' => 'crtpost', 'uses' => 'App\Http\Controllers\create_post@store'])->middleware('auth');

the errors

The inputimg must be an image.

The inputimg must be a file of type: jpeg, png, jpg, gif, svg.

edit:

i commented this part

 $request->validate([
           'inputimg' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
       ]);

and this error comes out Call to a member function store() on null

0 likes
4 replies
ralphmrivera's avatar

Can you post the image somewhere? Also, what does your tag look like?

mohamadAbdelhady's avatar

it gives the same errors with any image the input tag

<input type="file"  directory  accept="image/*" style="display: none;" id="upload-img" class="form-control" name="inputimg">

my form tag

 <form method="POST" action="{{route('crtpost')}}"
anilkumarthakur60's avatar
Level 6

you might me missing enctype="multipart/form-data" in the form as



<form action="" enctype="multipart/form-data"></form>
1 like

Please or to participate in this conversation.