How can the PHP script be modified to rename uploaded images to prevent overwriting existing files with the same name?

When uploading images using a PHP script, there is a risk of overwriting existing files with the same name. To prevent this, you can modify the script to rename the uploaded images by appending a unique identifier, such as a timestamp or a random string, to the file name. This will ensure that each uploaded image has a unique filename and prevent any conflicts.

// Generate a unique filename by appending a timestamp
$timestamp = time();
$uploaded_file = $_FILES['file']['name'];
$file_extension = pathinfo($uploaded_file, PATHINFO_EXTENSION);
$new_filename = $timestamp . '.' . $file_extension;

// Move the uploaded file to the desired directory with the new filename
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $new_filename);

echo 'File uploaded successfully with new name: ' . $new_filename;