我只希望将jpg和png文件保存在laravel数据库中

时间:2019-04-26 09:30:04

标签: php jquery html ajax laravel

我想保存扩展名为jpg&png的文件,但遇到了麻烦。

我尝试image => 'required|mime:jpg'失败。

基本上,我想使用laravel存储扩展名为jpg和png的图像。这是我的代码:

public function addImages(Request $request)
{
    $errors = [
        'image' => 'Please Enter description',
    ];

    $this->validate($request, [
        'image' => 'required|max:3' //only allow this type extension file.
    ]);



    $product_id = request('product_id');
    $array = array();
    foreach($request->file('image') as $image)
    {
        //$allowedfileExtension=['pdf','jpg','png','docx'];
        $name=time() . '.' .$image->getClientOriginalName();
        $extension = $image->getClientOriginalExtension();
        if(!in_array($extension,$allowedfileExtension));
        {
        }

        $image->move(public_path().'/images/', $name);
        $img = "/public/images/".$name;   
        $id =  DB::table('product_image')->insert(array('product_id'=>$product_id,'image'=>$img));
    }

    //image upoad code
    //return $request->all();

    $addImages = $this->addMultipleImage($id,$array);

    Session::flash('message', 'Image Added successfully!');

    return redirect("admin/edit-product/$product_id"); 
    }
}

告诉我,我要去哪里了?

1 个答案:

答案 0 :(得分:1)

尝试此验证

$this->validate($request, [
    'image' => 'required|mimes:jpg,png|max:50' //max size allowed will be 50kb
]);

或者尝试这个

$this->validate($request, [
  'image' => 'image|required|mimes:png,jpg|max:50'
]);
相关问题