What potential pitfalls should be avoided when implementing a file counting function in PHP?

One potential pitfall to avoid when implementing a file counting function in PHP is not properly handling errors that may occur during the file reading process. It is important to check if the file exists and is readable before attempting to count its contents. Additionally, make sure to close the file handle after reading to free up system resources.

function countFileLines($filename) {
    if(!file_exists($filename) || !is_readable($filename)) {
        return false;
    }

    $file = fopen($filename, 'r');
    if(!$file) {
        return false;
    }

    $lineCount = 0;
    while(!feof($file)) {
        fgets($file);
        $lineCount++;
    }

    fclose($file);

    return $lineCount;
}