Mokrani's avatar

Uploading img error

Hello Guys

i'm trying to a upload an image but when i click in validate problem it's show me an error page with no message

This my controller

public function uploadimg (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');
    }
}

My form

<form enctype="multipart/form-data" action="/client/uploadimg" method="POST"   >
                
                   {{{ csrf_field() }}}

My route

Route::post ('uploadimg','Client\ProfileController@uploadimage');
​```
***
Thank you so much for ur help
0 likes
6 replies
guybrush_threepwood's avatar

Hi @mokrani,

When you submit the form are you providing the input_img file or are you sending an empty form to test the validation?

When you say that you're getting an error page with no message do you mean a blank page? Or are you getting the form back but you can't see the validation messages?

The only thing wrong I noticed is this line in the controller that seems out of place:

$this->save();

Also, the form action isn't exactly the same as your route's path, but I'm guessing that's because you have a route group that's adding a prefix.

It would be nice to be able to look at the full form, to check the file input too.

Regards.

Mokrani's avatar

Hello Thank you for your reply

This is my full form

    <form enctype="multipart/form-data" action="/client/uploadimg" method="POST"   >
                
                   {{{ csrf_field() }}}
               <div class="text-center">
                 <h5 class="h3">
                   Jessica Jones<span class="font-weight-light">, 27</span>
                 </h5>
                 <div class="h5 font-weight-300">
                   
                 </div>
                 <div class="h5 mt-4">
                   
                 </div>
                 <div>
                   <i class="ni education_hat mr-2"></i>University of Computer Science
                 </div>
                 <main class="main_full">
                    <div class="container">
                        <div class="panel">
                            <div class="button_outer">
                                <div class="btn_upload">
                                    <input type="file" id="input_img" name="input_img">
                                    Upload Image
                                </div>
                                <div class="processing_bar"></div>
                                <div class="success_box"></div>
                            </div>
                        </div>
                        <div class="error_msg"></div>
                        <div class="uploaded_file_view" id="uploaded_view">
                            <span class="file_remove">X</span>
                        </div>
                    </div>
                </main>
                 
               </div>
             </div>
           </div>
           <button type ="submit" class=" validatebtn btn btn-success" data-toggle="sweet-alert" data-sweet-alert="success">Valider</button>
           <form>

When i click the button its show me a laravel error page it's say MethodNotAllowedHttpException No message!

And Yes i have a prefix for the route

Snapey's avatar

Your controller method is uploadimg

public function uploadimg (Request $request ) {

but in the route, the method being used is uploadimage

Route::post ('uploadimg','Client\ProfileController@uploadimage');

You also don't seem to be saving the name of the uploaded file anywhere

2 likes
Mokrani's avatar

i already noticed this but it's the same problem :( i also checked the php.ini file for max upload file ! :(

Snapey's avatar

So you are saying you fixed that error but you still get method not allowed?

Better check php artisan route:list Also run php artisan route:clear incase you have cached the routes

1 like
nolros's avatar

@mokrani building on Snapey reply. Try this code. Laravel has a StoreAs helper for storage. storeAs method, which accepts the path, file name, and disk name as its arguments. We will just path images and filename as you format


  public function uploadimage(Request $request)
  {
    // removing validation for brevity sake
    if ($request->has("input_img")) {
      $image = $request->input_img;
      return $image->storeAs('images', (time().'.'.$image->getClientOriginalExtension())) 
        ? "Image Upload successfully"
        : "Oops something went wrong";
    }
  }

Please or to participate in this conversation.