In PHP, what methods can be used to securely assign unique file names, such as UUIDs, during file uploads to prevent naming conflicts and security risks?

When uploading files in PHP, it is crucial to assign unique file names to prevent naming conflicts and potential security risks such as overwriting existing files or executing malicious scripts. One way to achieve this is by generating UUIDs (Universally Unique Identifiers) for each uploaded file. This ensures that each file has a unique identifier, reducing the risk of conflicts.

// Generate a UUID for the file name
$uniqueFileName = uniqid() . '-' . bin2hex(random_bytes(8));

// Move the uploaded file to the desired directory with the unique file name
move_uploaded_file($_FILES['file']['tmp_name'], '/path/to/uploaded/files/' . $uniqueFileName);