php - laravel 上传文件时出现错误

标签 php laravel laravel-5 intervention

我正在尝试在 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/

相关文章:

php - 如何从mysql中的多个表中按日期顺序获取数据?

php - Nginx 添加 headers PHP FPM 返回错误

php - 获取包含最新嵌套对象的前 5 个文档

javascript - 如何显示评分星级 Laravel 和 Ajax

Laravel 4.2 检查 whereIn 子句中的数组是否为空

php - Express Web 框架中 session key 的重要性

PHP 环境变量转移到子进程中

php - 在 Laravel 中减去时间

未创建 Laravel 命名空间

php - 在 Laravel 5 中为 Mailgun 添加 Guzzle