繁体   English   中英

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

[英]i want only jpg and png file save in database in 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"); 
    }
}

告诉我,我要去哪里错了?

试试这个验证

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM