What are some best practices for selecting and renaming random files in PHP?

When selecting and renaming random files in PHP, it is important to ensure that the new file names are unique to avoid overwriting existing files. One way to achieve this is by using functions like uniqid() or md5() to generate unique file names. Additionally, it is recommended to include a timestamp in the file name to further increase its uniqueness.

// Generate a unique file name using uniqid() and timestamp
$randomFileName = uniqid() . '_' . time() . '.jpg';

// Rename the file with the new unique name
rename('original_file.jpg', $randomFileName);