简体   繁体   中英

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.

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!

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

You only need to use this:

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

instead of:

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

Hope this will fixed your problem!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM