简体   繁体   中英

i want only jpg and png file save in database in laravel

I want to save files with extensions jpg & png but am having trouble.

I've tried image => 'required|mime:jpg' unsuccessfully.

Basically, I want to store images whose extension is jpg and png by using laravel. Here is my code:

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

Tell me, where I am going wrong?

Try this validation

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

Or try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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