Parth Sureliya
Parth Sureliya

Reputation: 256

get error while file upload in laravel

I am trying to upload image in laravel. But when i upload it gives error.

Call to a member function getClientOriginalExtension() on null

Here is my Blade file form

{{Form::open(array('url' => '/AddNewBlog','id'=>'blogadd' ,
   'method' => 'post','class'=>'form-row','files'=>true,
   "enctype"=>"multipart/form-data"))}}

And here is controller

 $imagename_bg = time() . '.' . $photo->getClientOriginalExtension();
 $destinationPath = public_path('/uploads/blog');
 $thumb_img = Image::make($photo->getRealPath())->resize(750, 450);
 $thumb_img->save($destinationPath . '/' . $imagename_bg, 80);
 $photo->move($destinationPath, $imagename_bg);

Please help me how to resolve this problem.

Upvotes: 3

Views: 7459

Answers (3)

Hiren Gohel
Hiren Gohel

Reputation: 5041

You only need to use this:

$photo = $request->file("blogimage");

instead of:

$photo = $request->input("blogimage")

Hope this will fixed your problem!

Upvotes: 1

kunal
kunal

Reputation: 4248

I am not able to understand your code. if you are looking for uploading image and resize using intervention package try this:-

if($request->hasFile('blogimage')){
if (Input::file('blogimage')->isValid()) {
    $file = Input::file('image');
    $img = Image::make($file);
    $img->resize(400,270);
    $name = pathinfo($_FILES['image']['name']);
    $destination = public_path('/uploads/blog');
    $ext = $name['extension'];
    $rand= time().str_random(6).date('h-i-s').'.'.$ext;
    $img->save($destination.$rand);
}
}

Or Without intervention pacakge:-

if($request->hasFile('blogimage')){
if (Input::file('blogimage')->isValid()) {
    $file = Input::file('blogimage');
    $destination = public_path('/uploads/blog');
    $ext= Input::file('blogimage')->getClientOriginalExtension();
    $mainFilename =time().str_random(5).date('h-i-s');
    $file->move($destination, $mainFilename.".".$ext);
}
}

Hope it helps!

Upvotes: 3

Kuldeep Mishra
Kuldeep Mishra

Reputation: 4050

$photo = $request->file('file_name');

Upvotes: 1

Related Questions