Shahrukhlaravel's avatar

Image

I want to know if this approach to upload image is right or not?

public function store(Request $request){ if($request->image) { $image = $request->file('image')->getClientOriginalName(); $request->file('image')->move('public/img',$image);

}

}

0 likes
8 replies
Shahrukhlaravel's avatar

public function store(Request $request){ if($request->image) { $image = $request->file('image')->getClientOriginalName(); $request->file('image')->move('public/img',$image);

}

}

Snapey's avatar

Your solution might work but its a bit simplistic

Any file upload with the same name as another will overwrite the first file.

You should also validate the input and make sure that it is only the type of files you want to accept.

Shivamyadav's avatar

@shahrukhlaravel do this

if( $file = $request->file('image)) {
	$name = $file()->getClientOriginalName();
	$store = move('images', $name);
	//then here your create or update logic goes
}

Please or to participate in this conversation.