What best practices should be followed when renaming uploaded files in PHP?

When renaming uploaded files in PHP, it is important to follow best practices to ensure security and prevent conflicts with existing files. One common approach is to generate a unique filename using a combination of timestamp and a random string. This helps to avoid overwriting existing files and makes it harder for malicious users to guess the file names.

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

// Generate a unique filename
$extension = pathinfo($originalFileName, PATHINFO_EXTENSION);
$newFileName = uniqid() . '_' . time() . '.' . $extension;

// Move the uploaded file to a new location with the unique filename
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $newFileName);