What are the recommended methods for renaming files during the upload process in PHP?

When uploading files in PHP, it is recommended to rename the files to avoid naming conflicts and improve security. One common approach is to generate a unique filename using a combination of timestamp and a random string. This can help prevent overwriting existing files and make it harder for malicious users to predict file paths.

// Generate a unique filename for uploaded files
$timestamp = time();
$randomString = bin2hex(random_bytes(5));
$originalFilename = $_FILES['file']['name'];
$extension = pathinfo($originalFilename, PATHINFO_EXTENSION);
$newFilename = $timestamp . '_' . $randomString . '.' . $extension;

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