Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP: Count the lines of a file
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
You can’t perform that action at this time.