Created
October 29, 2012 08:28
-
-
Save Macagare/3972347 to your computer and use it in GitHub Desktop.
PHP: Count the lines of a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function countLines($filePath, $emptyLines = true) { | |
$lineCounts = 0; | |
if (is_file($filePath) == true) { | |
$fileHandle = fopen($filePath, 'r'); | |
if ($fileHandle == true) { | |
while(feof($fileHandle) == false) { | |
$currentLine = fgets($fileHandle); | |
if ($emptyLines == false) { | |
if (trim($currentLine) != null) { | |
$lineCounts = $lineCounts + 1; | |
} | |
} else { | |
$lineCounts = $lineCounts + 1; | |
} | |
} | |
} | |
fclose($fileHandle); | |
} | |
return $lineCounts; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment