How can PHP be used to dynamically generate unique file names for uploaded files to avoid naming conflicts?

When uploading files to a server, naming conflicts can occur if multiple users upload files with the same name. To avoid this issue, PHP can be used to dynamically generate unique file names for uploaded files. This can be achieved by appending a timestamp or a random string to the original file name before saving it on the server.

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

// Generate a unique file name
$uniqueFileName = uniqid() . '_' . $originalFileName;

// Move the uploaded file to a specific directory with the unique file name
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $uniqueFileName);