What are some common mistakes to avoid when implementing text file upload and display functionality in PHP?

One common mistake to avoid when implementing text file upload and display functionality in PHP is not properly sanitizing user input before processing the uploaded file. This can lead to security vulnerabilities such as code injection or file manipulation. To prevent this, always validate and sanitize user input before handling the uploaded file.

// Validate and sanitize user input before processing the uploaded file
if(isset($_FILES['file'])) {
    $file = $_FILES['file'];
    
    // Validate file type
    $allowedExtensions = array('txt');
    $fileExtension = pathinfo($file['name'], PATHINFO_EXTENSION);
    
    if(!in_array($fileExtension, $allowedExtensions)) {
        echo "Invalid file type. Please upload a text file.";
        exit;
    }
    
    // Sanitize file content
    $fileContent = file_get_contents($file['tmp_name']);
    $sanitizedContent = htmlspecialchars($fileContent);
    
    // Display sanitized file content
    echo "<pre>" . $sanitizedContent . "</pre>";
}