reading a file line by line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

reading a file line by line

Post by pelegk2 »

how do u i read a file line by line and not at once?

[i have in the past posted here a question about it,and found the url using the search but i get the message that the message dosent exist any more]

thnaks i advance
peleg
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

bladecatcher
Forum Commoner
Posts: 67
Joined: Sat Mar 12, 2005 12:50 am

Post by bladecatcher »

I'll try but be warned newbie lurks beneath.

Code: Select all

$wpage = fopen("www/myfile.txt", "r");
while(!feof($wpage)){
	$readline = trim(fgetss($wpage, 1024)) ;
                //do stufff
}
fclose($wpage);
trdesigns
Forum Newbie
Posts: 9
Joined: Wed Aug 03, 2005 11:36 am
Location: Canada

Post by trdesigns »

I'm not exactly sure whether this is what you need, but file() will read the file into an array, each element of which will be one line of the file.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Yeah but file() reads the entire file, he might only want the first X lines, or he might want to stop reading when a condition is met.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

jshpro2 wrote:Yeah but file() reads the entire file, he might only want the first X lines, or he might want to stop reading when a condition is met.
I don't think there is any way of reading only up to a certain point of the file. The whole file must be put into memory first.
trdesigns
Forum Newbie
Posts: 9
Joined: Wed Aug 03, 2005 11:36 am
Location: Canada

Post by trdesigns »

Unless there is a fixed line length, for example each line containing exactly 80 characters, I don't think that can be done without loading the whole file into memory as Jcart said.
skhale
Forum Newbie
Posts: 6
Joined: Sat Aug 06, 2005 12:56 pm
Location: Boston, MA

Post by skhale »

anjanesh wrote:fgets()
php.net wrote:fgets -- Gets line from file pointer
php.net wrote:Reading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first).
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Jcart wrote:I don't think there is any way of reading only up to a certain point of the file. The whole file must be put into memory first.

fgets() bro...

anjanesh had said that already, I was just replying to the person who pointed out file() and I was saying file() isn't anymore efficient then doing fgets(), and fgets() is more efficient in the case of a partial file.
Post Reply