What are common pitfalls when using fopen() to create a new file in PHP?

One common pitfall when using fopen() to create a new file in PHP is not checking if the file already exists before attempting to create it. This can result in overwriting existing files or encountering errors. To avoid this, you should first check if the file exists using file_exists() before attempting to create it.

$filename = 'example.txt';

if (!file_exists($filename)) {
    $file = fopen($filename, 'w');
    // additional code to write to the file
    fclose($file);
} else {
    echo "File already exists.";
}