上传时在php中覆盖图像

I want to know that how can I overwrite images when they uploaded to server in php. For example I uploaded a photo to a folder as soon as I upload another image it will take place of previous image. Image name is not same it may differ. Thanks

Delete the previous image before uploading the new one using:

$path = $_SERVER['DOCUMENT_ROOT'].'img/img.jpg';
unlink($path);

This code basically assigns the path of the image to $path and deletes the image using unlink($path);