asdasdsa's avatar

Save/store file to path, but don't include the directory?

Hi!

Im trying to save files from a form. And it seems to work! The files appear in the correct folder as I wish, but another problem has encountered; that the directory name included in when saving to database.

        if($path = Storage::putFile('public', $request->file('photo'))){
            $question->image = $path;
            $question->save();
        }else{
            return response()->json('failed to upload the image', 500);
        }

This results in public/sadasdsdfadf.png but I just want sadasdsdfadf.png to be stored. How can I achieve this?

0 likes
3 replies
hupp's avatar

You can take file name as separate and store that file name on database for retrieve that particular image

$image = $request->image;
$logged_in_userid = $request->user_id;
$filename = $logged_in_userid.'_'.time().rand(0,9999).'.'.$image->getClientOriginalExtension();
 $path = $request->file('image')->storeAs('public/users',$filename);     
  $result = UserMedata::where('user_id',$logged_in_userid)->update(['profile_image' =>$filename]);

you can do something like this for store only file name on database

Please or to participate in this conversation.