簡體   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