osherdo's avatar

Error On Image Upload[L5.2]

Hello. I am using Laravel 5.2. I am getting this error, when uploading image to the server:

Call to a member function getClientOriginalExtension() on a non-object

I have this line that casuses the issue:

            $extension      =   $image->getClientOriginalExtension();

I am using intervention standard function to process image upload:

    public function save(Request $request)
    {
        $image          =   $request->file('image');
        $resizedImage   =   $this->resize($image, $request->get('image_size'));

        if(!$resizedImage)
        {
            return redirect()->back()->withError('Could not resize Image');
        }
        return redirect()->route('image.resized')->with('image_url', asset('images/user_images'). '/' .$resizedImage->basename);
    }
    public function show()
    {
        $image_url = session('image_url');

        return view('images.show', compact('image_url'));
    }

    private function resize($image, $size)
    {
        try
        {
            $extension      =   $image->getClientOriginalExtension();
            $imageRealPath  =   $image->getRealPath();
            $thumbName      =   'thumb_'. $image->getClientOriginalName();

            //$imageManager = new ImageManager(); // use this if you don't want facade style code
            //$img = $imageManager->make($imageRealPath);

            $img = Image::make($imageRealPath); // use this if you want facade style code
            $img->resize(intval($size), null, function($constraint) {
                 $constraint->aspectRatio();
            });
            return $img->save(public_path('images/user_images'). '/'. $thumbName);
        }
        catch(Exception $e)
        {
            return false;
        }

    }

My form does have enctype="multipart/form-data" in the markup:

    <form class="box" method="post" action="" enctype="multipart/form-data"> <!--this enctype is used to handle POST request with input type="file" -->

So I am trying to figure out what going on. the file name seems to show in the view when selecting it.

Would appreciate you help, thanks.

0 likes
6 replies
osherdo's avatar

with this given code:

public function save(Request $request)
    {
      if ($request->hasFile('image')) {
          dd("Have image");
      }
      else{
      dd("Does not have image");
      }
    }

It returns:

"Does not have image"

I really don't get why the image is not included in the request.

tomopongrac's avatar
Level 51

@osherdo

How you view file is looking ... it looks like you input name is not right ...

1 like
osherdo's avatar

@tomi thanks for replying. form for image upload:

<form class="box" method="post" action="" enctype="multipart/form-data"> <!--this enctype is used to handle POST request with input type="file" -->
      <input type="hidden" name="_token" value="{{ csrf_token() }}">
  <div class="box__input">
    <input class="box__file" type="file" name="files[]" id="file" data-multiple-caption="{count} files selected" multiple /> <!-- Shows in case drag and drop is not supported.  -->
    <label for="file"><strong>Choose a file</strong><span class="box__dragndrop"> or drag it here</span>.</label> <!-- Shows in case drag and drop is supported.  -->
    <button class="box__button" type="submit">Upload</button>
  </div>
  <div class="box__uploading">Uploading…</div> <!-- Shown during image upload. -->
  <div class="box__success">Done!</div> <!-- Shown when image upload done. -->
  <div class="box__error">Error! <span></span>.</div> <!-- Shown when image upload failed. -->
</form>
Mo7sin's avatar

As @tomi said, your field name is files[] and your checking for image

1 like
osherdo's avatar

@Mo7sin thanks. @tomi thanks for pointing that out. Unfortunately I cannot see your reply here, and I saw partially on email only, so I could not test your suggestion until @Mo7sin quoted you. It was the issue. thanks.

if I want to get the images to upload as an array - I should change to files[] in both the view and controller. is that correct?

Please or to participate in this conversation.