views:

70

answers:

4

Hi there,

as I was not able to find a function which retrieves the amount of lines a file has, do I need to use

$handle = fopen("file.txt");

For($Line=1; $Line<=10; $Line=$Line+1){
 fgets($handle);
}

If feof($handle){
 echo "File has 10 lines or more.";
}Else{
 echo "File has less than 10 lines.";
}

fclose($handle)

or something similar? All I want to know is if the file has more than 10 lines or not :-).

Thanks in advance!

+3  A: 

You can get the number of lines using:

$file = 'smth.txt';    
$num_lines = count(file($file));
rpSetzer
keep in mind though, that this will read in the entire file into memory, which might not be desirable with very large files.
Gordon
Thank you, rpSetzer and Gordon for the heads up.
Chris
+2  A: 

Faster, more memory resourceful:

$file = new SplFileObject('file.txt');
$file->seek(9);
if ($file->eof()) {
 echo 'File has less than 10 lines.';
} else {
 echo 'File has 10 lines or more.';
}

SplFileObject

webbiedave
Thank you for your reply, might use it later but the file I'm using has only 1-12 lines :-).
Chris
@webbiedave I think this should be *"less than or 10 lines"* and *"more than 10 lines"*
Gordon
+1  A: 

This bigger problems will occur if you have a LARGE file, PHP tends to slow down some. Why not run an exec command and let the system return the number? Then you do not have to worry about the PHP overhead to read the file.

$count = exec("wc -l /path/to/file");

Or if you want to get a bit more fancy:

$count = exec("awk '// {++x} END {print x}' /path/to/file");
cdburgess
No explanation for the down vote?
cdburgess
I upvoted you but you should keep in mind that not everyone uses PHP under Linux.
mhitza
I get that. But what does that have to do with the solution? This will work on Linux/Mac/FreeBSD/UNIX etc. The only issue would be with Windows. That being said, I think this is still an elegant solution, especially considering there was no OS specified. I don't think it warranted a down vote.
cdburgess
I didnt downvote but when using platform specific solutions, why not add it as a note to the answer. Btw, this should work on Windows with Cygwin tools installed.
Gordon
This answer is silly. First you can make PHP read only the first X lines without having to read the whole file. Second how is incurring the overhead of creating a new process and making that process read the whole LARGE file better?
Moron
@Gordon: I assumed it was obvious. It seems everyone is recognizing what platform it is written for. @Moron: The difference is if you try to read an extremely large file using file() in PHP, it will use lots of memory. The solution I provided will run much faster with less overhead. Take a 5gb file and try counting line with file() and see what happens. It wasn't until a later comment he indicated the file will only be 1-12 lines long.
cdburgess
@cdbur: Are you saying PHP provides no way to read the initial bytes of a file without reading the whole file? I don't buy it.
Moron
@Moron: Don't put words in my mouth. I am saying that the use of file() and defined in the selected best answer will choke with large file sizes. It works for webbiedave's purposes, but in circumstances where the file sizes are large, it will cause issues.
cdburgess
@Cdbur: I am not putting words into your mouth. "PHP tends to slow down" is a much more generic statement than saying a specific method is slow. The OP might be using a slow method, but he says "or something similar". In any case, even when compared to file() (assuming it is slow), creating a new process and reading the whole file will be just as bad for LARGE files.
Moron
@Moron: Hair splitting aside, if you do not believe what I am saying, why not benchmark it? I ran time comparisons on `echo count(file("FILE"));` and echo `exec("wc -l FILE");` for the same file with 354516 lines. I used `time php filetest.php` on my Mac. Guess which method took three times as long to run?
cdburgess
@cdb: And how does that compare to webbiedave's solution?
Moron
@Moron: His code ran 6 times faster than the selected solution. SO I find it funny that the best answer has the worse latency.
cdburgess
@cdb: I hope you get the point I was trying to make. Sorry if I offended you in some way.
Moron
@Moron: Nope. No offense here. It all started with a down vote without an explanation. I would still like to know why it was down voted though. Especially in light of the fact that even though this solution is not cross platform compatible, and without replicating what the original post supplied, it still runs faster than the winning solution. Things that make you say hmmmm.
cdburgess
A: 

If you have big file then better schould be read files in segments and counts "\n" chars, or what ever is the lineend char, for example on some systems you will also need "\r" counter or whatever...

$lineCounter=0;
$myFile =fopen('/pathto/file.whatever','r');
   while ($stringSegment = fread($myFile, 4096000)) {
$lineCounter += substr_count($stringSegment, "\n");
}
kensai