What are potential pitfalls when handling multiple files with the same name in PHP?

When handling multiple files with the same name in PHP, a potential pitfall is overwriting existing files unintentionally. To avoid this, you can append a unique identifier to each file name before saving it to the filesystem. This ensures that each file has a distinct name and prevents any conflicts.

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

// Append the unique identifier to the file name
$file_name = 'example.txt';
$unique_file_name = $file_name . '_' . $unique_id;

// Save the file with the unique name
file_put_contents($unique_file_name, $file_contents);