ziben69's avatar

Image multiple upload - same photos

Hello guys,

when I load more than 1 photos, I get 4 of the same. What may be the problem. The method below:

 public function store(Request $request) {
        $event = new Event;

        if($event->save()) {
            if ($request->has('photos')){
                foreach ($request->photos as $photo) {  
                    $filename = 'img-'.time().'.'.$photo->getClientOriginalExtension();
                    $photo->storeAs('public/photos/',$filename);
                    Photo::create([
                        'event_id' => $event->id,
                        'filename' => $filename
                    ]);
                }
            }
	}

by uploading 4 different photos the result is 4 photos the same as the first. Thanks for help

0 likes
2 replies
MichalOravec's avatar
Level 75

time() return time in seconds so your foreach run during one second, this is the reason why you get same name.

$filename = 'img-'.Str::random(14).'.'.$photo->getClientOriginalExtension();
1 like

Please or to participate in this conversation.