Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Macagare
Created October 29, 2012 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Macagare/3972347 to your computer and use it in GitHub Desktop.
Save Macagare/3972347 to your computer and use it in GitHub Desktop.
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