What steps can be taken to prevent file overwriting issues when multiple users upload files with the same name in PHP?

When multiple users upload files with the same name in PHP, there is a risk of file overwriting. To prevent this issue, you can append a unique identifier to the file name before saving it to the server. This can be achieved by generating a random string or using a timestamp to ensure each file has a distinct name.

// Generate a unique identifier
$unique_id = uniqid();

// Get the original file name
$filename = $_FILES['file']['name'];

// Append the unique identifier to the file name
$unique_filename = $unique_id . '_' . $filename;

// Save the file with the unique filename
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $unique_filename);