Call to a member function getClientOriginalName() on a non-object with intervent...

public function postCreate()
{
    $validator = Validator::make(Input::all(), Product::$rules);

    if ($validator->passes()){
        $product = new Product;
        $product->category_id = Input::get('category_id');
        $product->title = Input::get('title');
        $product->description = Input::get('description');
        $product->price = Input::get('price');

        $image = Input::file('image');
        $filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
        Image::make($image->getRealPath())->resize(468, 249)->save('public/image/products/'. $filename);
        $product->image = 'image/products/ '. $filename;
        $product->save();

        return Redirect::to('admin/products/index')
        ->with('message','<i class="fa fa-check-square-o fa-lg"></i> Products has been created!');
    }
        return Redirect::to('admin/products/index')
        ->with('message', '<i class="fa fa-exclamation-triangle fa-lg"></i> Something went wrong! ')
        ->withErrors($validator)
        ->withInput();


}
cilsilver
cilsilver
  • 3 years ago

mmm, what returns Input::hasFile( 'image' ) ??

also, in your form setup, you need to specified the files attribute

{{ Form::open( [ 'url' => '...', 'method' => 'post', 'files' => true ] ) }}

Thanks arcollector for helping, but this brought me back to the same error after your modification. can you go through the whole code? thanks so much.

I found this error before. My PHP.ini setting were the culprit for this. Post_max_size or upload_max_size werent allowing the image to processed as the image file size broke these settings.

PS the return for hasFile() is a boolean not the file itself.

matthewburrow said:

I found this error before. My PHP.ini setting were the culprit for this. Post_max_size or upload_max_size werent allowing the image to processed as the image file size broke these settings.

PS the return for hasFile() is a boolean not the file itself.

Thanks matthew, I will give it a try now.

cilsilver said:

matthewburrow said:

I found this error before. My PHP.ini setting were the culprit for this. Post_max_size or upload_max_size werent allowing the image to processed as the image file size broke these settings.

PS the return for hasFile() is a boolean not the file itself.

Thanks matthew, I will give it a try now.

I did as advised but giving a new error that " Intervention \ Image \ Exception \ ImageNotWritableException " what could have caused this again?

The new error is Intervention \ Image \ Exception \ ImageNotWritableException. Can someone help out. I've been stuck with this for 4days now. Thanks

Are you sure the directory public_path().'/img/products/ exists? Do you have permission to write to it? If not, try a 777 chmod to products/

Yes it exists. I'm developing locally on windows. its also case sensitive,

Hi all !

I have also the same problem but with another function : Call to a member function getClientOriginalExtension() on a non-object

But everything is fine, my image is uploaded I can create a thumbnail and my function send me a 200 Response.

My form has a files => true but can't figure why this error is showing.

My function

    public function imageUpload(){

        $file = Input::file('image');
        $soliste = Soliste::findOrFail(Input::get('id'));


        $input = array('image' => $file);
        $rules = array(
            'image' => 'image'
        );
        $validator = Validator::make($input, $rules);
        if ( $validator->fails() )
        {
            return Response::json(['success' => false, 'errors' => $validator->getMessageBag()->toArray()]);

        }
        else {

            $destinationPath = 'uploads/avatars/';
            $old_image = $destinationPath.$soliste->avatar;
            $old_preview = 'preview_'.$old_image;

            // Deleting old images - preview & original
            if(File::isFile($old_image) && File::isFile($old_preview)){
                File::delete($old_image);
                File::isFile($old_preview)
            }

            $filename = Sanitize::string($soliste->firstname).'-'.Sanitize::string($soliste->lastname).'-'.str_random(5).'.'.$file->getClientOriginalExtension();

            // Upload Original version
            $file->move($destinationPath, $filename);

            // create a preview to speed-up upload and keep aspect ratio
            $preview = Image::make($destinationPath.$filename);
            $preview->resize(500,null,true);
            $preview_filename = $destinationPath.'preview-'.$filename;
            $preview->save($preview_filename);

            // saving model
            $soliste->avatar = $destinationPath.$filename;
            $soliste->save();

            return Response::json(['success' => true, 'file' => asset($preview_filename)]);
        }
    }

I also made a var_dump of the file inout and everything seems fine

file var output

object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(45) "Capture d’écran 2014-03-12 à 10.47.13.png" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(9) "image/png" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(355823) ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) ["pathName":"SplFileInfo":private]=> string(36) "/Applications/MAMP/tmp/php/phptBWgOn" ["fileName":"SplFileInfo":private]=> string(9) "phptBWgOn"}{"success":true,"file":"http:\/\/localhost:8888\/solistes\/public\/uploads\/avatars\/preview-lucas-toumia-morissette-de-la-lavandiere-bKjkn.png"}

bidibule said:

Hi all !

I have also the same problem but with another function : Call to a member function getClientOriginalExtension() on a non-object

But everything is fine, my image is uploaded I can create a thumbnail and my function send me a 200 Response.

My form has a files => true but can't figure why this error is showing.

My function

  public function imageUpload(){

      $file = Input::file('image');
      $soliste = Soliste::findOrFail(Input::get('id'));


      $input = array('image' => $file);
      $rules = array(
          'image' => 'image'
      );
      $validator = Validator::make($input, $rules);
      if ( $validator->fails() )
      {
          return Response::json(['success' => false, 'errors' => $validator->getMessageBag()->toArray()]);

      }
      else {
          
          $destinationPath = 'uploads/avatars/';
          $old_image = $destinationPath.$soliste->avatar;
          $old_preview = 'preview_'.$old_image;

          // Deleting old images - preview & original
          if(File::isFile($old_image) && File::isFile($old_preview)){
              File::delete($old_image);
              File::isFile($old_preview)
          }

          $filename = Sanitize::string($soliste->firstname).'-'.Sanitize::string($soliste->lastname).'-'.str_random(5).'.'.$file->getClientOriginalExtension();

          // Upload Original version
          $file->move($destinationPath, $filename);
          
          // create a preview to speed-up upload and keep aspect ratio
          $preview = Image::make($destinationPath.$filename);
          $preview->resize(500,null,true);
          $preview_filename = $destinationPath.'preview-'.$filename;
          $preview->save($preview_filename);

          // saving model
          $soliste->avatar = $destinationPath.$filename;
          $soliste->save();
          
          return Response::json(['success' => true, 'file' => asset($preview_filename)]);
      }
  }

I also made a var_dump of the file inout and everything seems fine

file var output

object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) { ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> bool(false) ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(45) "Capture d’écran 2014-03-12 à 10.47.13.png" ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> string(9) "image/png" ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(355823) ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=> int(0) ["pathName":"SplFileInfo":private]=> string(36) "/Applications/MAMP/tmp/php/phptBWgOn" ["fileName":"SplFileInfo":private]=> string(9) "phptBWgOn"}{"success":true,"file":"http:\/\/localhost:8888\/solistes\/public\/uploads\/avatars\/preview-lucas-toumia-morissette-de-la-lavandiere-bKjkn.png"}

Really, it made me frustrated for days now on solving the issue. got your error which was why i changed option but still issues. on Linux, it seems to work. My file permissions are accurate on windows. what could have been this problem?

I do not want to reopen old threads and if i crossed a forum guideline my apologies, however I've found that if i do like you etc

$file = Input::file('image');

and then try to do either of these

$file->getClientOriginalExtension(); 
$file->getClientOriginalName();

it will throw that error however if I would do like

Input::file('image')->getClientOriginalExtension();

It would work just fine.

hello everyone, As i have solved this problem and i have made use in my project for uploading images.

public function store(Request $request) {

$input = $request->all(); if(isset($input['photo']))

{ $destinationPath = 'image'; // upload path $fileName = str_replace(' ','',$input['name']).''.rand(11111,99999).'_'.$request->file('photo')->getClientOriginalName(); $move_photo = $request->file('photo')->move(base_path(). '/'.$destinationPath, $fileName); $allPath = $move_photo; $type = pathinfo($allPath, PATHINFO_EXTENSION); $data = file_get_contents($allPath); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); $input['photo'] = $base64;

}

else {

$input['photo'] = null;
}

$files = new Files;
$files->photo = $input['photo'];
$files->save();

}