What potential pitfalls should be considered when using PHP to manipulate text files?

One potential pitfall when using PHP to manipulate text files is not properly handling file permissions, which can lead to issues with reading or writing to the file. To avoid this, ensure that the file has the correct permissions set before attempting any file operations.

// Check file permissions before reading or writing
if (is_readable('example.txt') && is_writable('example.txt')) {
    // Perform file operations here
    $file = fopen('example.txt', 'r');
    // Read file contents
    fclose($file);
} else {
    echo "File does not have proper permissions.";
}