What are the potential pitfalls when using the fopen() function in PHP?

One potential pitfall when using the fopen() function in PHP is not checking if the file was successfully opened before proceeding with reading or writing operations. This can lead to errors or unexpected behavior if the file does not exist or cannot be opened. To solve this issue, you should always check the return value of fopen() to ensure that the file was opened successfully before performing any operations on it.

$file = fopen("example.txt", "r");
if($file) {
    // File opened successfully, proceed with reading operations
    fclose($file);
} else {
    // Handle error opening file
    echo "Error opening file";
}