简体   繁体   中英

Move uploaded file and save to database with unique name

I am trying to save fine path in database and save image file in folder but I want to save file with unique name. How to do that?

My controller is :

$image = $request->photo;
//$photoname_path = 'images'.$date.'/'.$image->getClientOriginalName();
$image->move('images',$image->getClientOriginalName());
$photoname = 'images/'.$image->getClientOriginalName();

$adduser = new Employee;
$adduser->name = $request->name;
$adduser->photo = $photoname;
$adduser->email = $request->email;
$adduser->contact_no = $request->contact_no;
$adduser->pan_no = $request->pan_no;
$adduser->department = $request->department;
$adduser->position = $request->position;
$adduser->save();
return redirect()->back();

I was trying to save it with time but I failed. How to do it?

I see that you have access to the request object so, try this:

    $photo = $request->file('photo');
    $photoName = time() . '_' . $photo->getClientOriginalName();
    $photo->move('images', $photoName);

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