【问题标题】:get error while file upload in laravel在laravel中上传文件时出错
【发布时间】:2018-05-18 17:18:24
【问题描述】:

我正在尝试在 laravel 中上传图片。但是当我上传时它会出错。

在 null 上调用成员函数 getClientOriginalExtension()

这是我的 Blade 文件格式

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

这里是控制器

 $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);

请帮我解决这个问题。

【问题讨论】:

  • $photo 是什么?
  • 这是我发布的变量的名称
  • 您正在使用干预包?
  • dd($photo);看看结果好吗!
  • $photo = $request->photo 吗?

标签: php laravel laravel-5.2 intervention


【解决方案1】:
$photo = $request->file('file_name');

【讨论】:

    【解决方案2】:

    我无法理解您的代码。如果您正在寻找使用干预包上传图片和调整大小,请尝试以下操作:-

    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);
    }
    }
    

    希望对你有帮助!

    【讨论】:

    • 感谢兄弟的工作。你能告诉我如何调整这张图片的大小
    • 你安装了intevention包吗? @ParthSureliya
    • 然后按照我的第一个答案与干预 pcakage 获取更多信息:- image.intervention.io/api/resize
    【解决方案3】:

    你只需要使用这个:

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

    代替:

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

    希望这能解决您的问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 2017-09-03
      • 1970-01-01
      • 1970-01-01
      • 2020-01-29
      • 1970-01-01
      • 2018-01-14
      相关资源
      最近更新 更多