How can multiple uploaded files be given unique names using PHP?

When multiple files are uploaded using PHP, it is important to ensure that each file has a unique name to prevent overwriting. One way to achieve this is by appending a timestamp or a unique identifier to the file name before saving it.

<?php
// Loop through each uploaded file
foreach ($_FILES['file']['name'] as $key => $name) {
    $unique_name = time() . '_' . $name; // Append timestamp to the original file name
    $target_path = "uploads/" . $unique_name; // Specify the target directory
    move_uploaded_file($_FILES['file']['tmp_name'][$key], $target_path); // Move the uploaded file to the target directory
}
?>