[PHP] Getting line count of a text file
Hi all, In PHP 5, I'm counting the number of lines in a text file using the following code: $myfile = file ( '/path/to/myfile.txt'); $count = count ($myfile); However, sometimes it fails with the following error: PHP Fatal error: Allowed memory size of bytes exhausted (tried to allocate 35 bytes) in /path/to/myprogram.php on line 16 Line 16 is the first line above. I'm sure there is a better - more memory efficient - way to do this. Any suggestions? Thanks! Frank Schima Foraker Design http://www.foraker.com
Re: [PHP] Getting line count of a text file
On Sep 12, 2007, at 1:46 PM, Nathan Nobbe wrote: On 9/12/07, Wolf [EMAIL PROTECTED] wrote: $command= escapeshellcmd(cat {$file} | wc -l); // Run command and capture results $count = exec($command); that would work on *nix, but not windows. Since I am developing and deploying on *nix, shell commands are fine with me. Frank J. Schima [EMAIL PROTECTED] wrote: Hi all, In PHP 5, I'm counting the number of lines in a text file using the following code: $myfile = file ( '/path/to/myfile.txt'); $count = count ($myfile); However, sometimes it fails with the following error: PHP Fatal error: Allowed memory size of bytes exhausted (tried to allocate 35 bytes) in /path/to/myprogram.php on line 16 Line 16 is the first line above. I'm sure there is a better - more memory efficient - way to do this. Any suggestions? Thanks! Frank Schima Foraker Design http://www.foraker.com if you want a cross-platform solution (not that i use windows :)) or you dont want to use exec() or related functions look to the stream functions. they support buffered reading, so theorectically a script could read portions of a file and count lines as it read through the portions. Sounds a little painful. I'll look into it though. -nathan Thanks! Frank Schima Foraker Design http://www.foraker.com 303-449-0202
Re: [PHP] Getting line count of a text file
Hi Gang, On Sep 12, 2007, at 1:58 PM, bruce wrote: if you're going to do the system approach, go ahead and use awk/cut... wc -l foo.txt | awk -F' ' '{print $1}' should work assuming you're on linux. I have solved the problem with this approach. Thanks for all the suggestions. FYI, it was a (very) minor pain to make that into a string. I ended up using this: $count = system(wc -l /path/to/myfile.txt | awk -F' ' '{print . '$' . 1}'); I didn't need to escape anything because the file was hardcoded - no user input. Cheers, Frank Schima Foraker Design http://www.foraker.com 303-449-0202
Re: [PHP] PHP 5.2.0 Session Handling Bug? Can someone test this please?
The ID never changed for me. PHP 5.2.0 Apache 1.3.33 Mac OS X 10.4.8 On Nov 16, 2006, at 9:28 AM, Colin Guthrie wrote: I've noticed a bug with PHP 5.2.0 when dealing with sessions and posting quite large forms. I've attached a file that highlights the bug. Can people test this please and if it is confirmed i'll post upsteam. Just want to rule out my distro's packaging being at fault I am using the suochsim, soouishm, soushim ach the security patch thing, so that may be what is at fault here. Anyway, I don't think the data itself is to blame but I didn't fiddle too much with it as I didn't see the point. I've tested this on PHP 5.1.6 and it is not affected. Thanks. Col. bug.php Frank Schima Foraker Design http://www.foraker.com 303-449-0202