Php 如何使用所需名称在laravel中上载文件

Php 如何使用所需名称在laravel中上载文件,php,laravel,Php,Laravel,上载文件时,我遇到一个错误,它表示未定义的变量:filenametostore 这是我的控制器: if(!$validator->fails()){ $cate = Product::where('name','=',$request->product_name)->first(); if($cate){ $arr = array('status'=>'false-1','message'=>'Product name already

上载文件时,我遇到一个错误,它表示未定义的变量:
filenametostore

这是我的控制器:

if(!$validator->fails()){
    $cate = Product::where('name','=',$request->product_name)->first();
    if($cate){
        $arr = array('status'=>'false-1','message'=>'Product name already exist');
    }else{
        $cat = new Product();
        $cat->name = $request->product_name;
        $cat->category_id = $request->product_category;
        $cat->brand_id = $request->product_brand;
        $cat->quantity = $request->product_quantity;
        $cat->buy_per_piece = $request->product_buying_per_piece;
        $cat->sell_per_piece = $request->product_selling_per_piece;
        if($request->hasFile('thumbnail')){
            $filenameExt = $request->file('thumbnail')->getClientOriginalName();
            $filename = pathinfo($filenameExt,PATHINFO_FILENAME);
            $extension = $request->file('thumbnail')->getClientOriginalExtension();
            $filenametostore = $filename.'_'.time().'.'.$extension;
            $request->file('thumbnail')->move(public_path('images'), $filenametostore);
        }
        $cat->image = $filenametostore;
        $cat->save();

        $arr = array('status'=>'true','message'=>'Successfully Inserted');
    }
}else{
    $arr = array('status'=>'false','message'=>$validator->errors()->all());
}

请帮助我纠正错误

如果($request->hasFile('thumbnail')的条件为false,则始终会出现有关未定义变量的错误。您能检查一下您的请求中是否真的有名为缩略图的文件(例如使用此名称的表单输入)否我有
dd($request->hasFile('缩略图'))
它说的是真的,如果
$cat->image=$filenametostore
那么文件没有上传,说明
filenametostore
没有定义移动
$cat->image=$filenametostore
if($request->hasFile('缩略图'))
条件的内部。@iamab.in也尝试过,那么文件不会被上传,而不是所有东西都被上传。将
dd($filenametostore)
作为
if($request->hasFile('缩略图'))条件的最后一行,并让我们知道结果