What are some best practices for implementing a multi-file upload feature using PHP?

When implementing a multi-file upload feature using PHP, it is important to handle file uploads securely and efficiently. One best practice is to validate the uploaded files to ensure they are of the correct type and size before processing them. Additionally, consider generating unique filenames for the uploaded files to prevent overwriting existing files. Finally, make sure to handle any errors that may occur during the upload process gracefully.

<?php
// Check if files were uploaded
if(isset($_FILES['files'])){
    $errors = [];
    $uploadedFiles = [];
    $uploadDir = 'uploads/';

    foreach($_FILES['files']['tmp_name'] as $key=>$tmp_name){
        $file_name = $_FILES['files']['name'][$key];
        $file_size = $_FILES['files']['size'][$key];
        $file_tmp = $_FILES['files']['tmp_name'][$key];
        $file_type = $_FILES['files']['type'][$key];

        // Validate file type and size
        // Process file upload if valid
        // Generate unique filename
        $uploadFile = $uploadDir . uniqid() . '_' . $file_name;

        if(move_uploaded_file($file_tmp, $uploadFile)){
            $uploadedFiles[] = $uploadFile;
        } else {
            $errors[] = "Error uploading $file_name";
        }
    }

    if(!empty($errors)){
        foreach($errors as $error){
            echo $error . '<br>';
        }
    }

    if(!empty($uploadedFiles)){
        echo 'Files uploaded successfully:';
        foreach($uploadedFiles as $file){
            echo '<br>' . $file;
        }
    }
}
?>