What potential pitfalls are associated with including a file in PHP when it comes to handling file uploads?

One potential pitfall associated with including a file in PHP when handling file uploads is the risk of exposing sensitive information or allowing unauthorized access to files. To mitigate this risk, it is important to properly validate and sanitize user input before including any files. Additionally, setting proper file permissions and using secure file upload methods can help prevent security vulnerabilities.

// Example code snippet to handle file uploads securely
if(isset($_FILES['file'])){
    $file_name = $_FILES['file']['name'];
    $file_tmp = $_FILES['file']['tmp_name'];
    
    // Validate file type and size
    $allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
    $max_file_size = 1048576; // 1MB
    $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);

    if(!in_array($file_extension, $allowed_extensions) || $_FILES['file']['size'] > $max_file_size){
        echo "Invalid file. Please upload a valid image file within 1MB.";
    } else {
        // Handle file upload securely
        move_uploaded_file($file_tmp, 'uploads/' . $file_name);
        echo "File uploaded successfully.";
    }
}