What are the potential pitfalls of using the move_uploaded_file() function in PHP for uploading multiple files?

Potential pitfalls of using the move_uploaded_file() function in PHP for uploading multiple files include overwriting existing files with the same name, potential security vulnerabilities if not properly sanitized, and the need for handling errors and file validation for each uploaded file.

// Loop through each uploaded file and move them to a designated directory
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name){
    $file_name = $_FILES['file']['name'][$key];
    $file_tmp = $_FILES['file']['tmp_name'][$key];
    
    // Check if file already exists in the destination directory
    if(file_exists('uploads/' . $file_name)){
        $file_name = time() . '_' . $file_name; // Append timestamp to avoid overwriting
    }
    
    // Move the uploaded file to the destination directory
    if(move_uploaded_file($file_tmp, 'uploads/' . $file_name)){
        echo 'File ' . $file_name . ' uploaded successfully.<br>';
    } else {
        echo 'Error uploading file ' . $file_name . '.<br>';
    }
}