我正在尝试在 laravel 中上传图像。但是当我上传时出现错误。
Call to a member function getClientOriginalExtension() on null
这是我的 Blade 文件表单
{{Form::open(array('url' => '/AddNewBlog','id'=>'blogadd' ,
'method' => 'post','class'=>'form-row','files'=>true,
"enctype"=>"multipart/form-data"))}}
这是 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);
请帮我解决这个问题。
最佳答案
我无法理解您的代码。如果您正在寻找上传图像并使用干预包调整大小,请尝试以下操作:-
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);
}
}
或者没有干预包:-
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);
}
}
希望对你有帮助!
关于php - laravel 上传文件时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47646960/