By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,241 Members | 760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,241 IT Pros & Developers. It's quick & easy.

PHP read large file line by line?

P: n/a
hi,

how to read a large php file line by line?

fread can't?

thanks.

Oct 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hmm howa <ho******@gmail.comwrote:
hi,

how to read a large php file line by line?

fgets

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this =mysql_query();
Oct 13 '06 #2

P: n/a

$file_array = file($file_name);

foreach ($file_array as $line_number =$line)
{
// Handle the line
}

See: http://us3.php.net/manual/en/function.file.php
howa wrote:
hi,

how to read a large php file line by line?

fread can't?

thanks.
Oct 13 '06 #3

P: n/a
ZabMilenko wrote:
>
$file_array = file($file_name);

foreach ($file_array as $line_number =$line)
{
// Handle the line
}

See: http://us3.php.net/manual/en/function.file.php
howa wrote:
>hi,

how to read a large php file line by line?

fread can't?

thanks.
The problem with your solution is it reads the entire file into the
array. Note he said LARGE FILE. This could very quickly run him out of
memory.

Ikciu has the correct answer. fgets() is the way to go.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 13 '06 #4

P: n/a
[ Please don't top-post. Read http://www.caliburn.nl/topposting.html ]

ZabMilenko top-posted (corrected):
howa wrote:
>hi,

how to read a large php file line by line?

fread can't?

thanks.

$file_array = file($file_name);

foreach ($file_array as $line_number =$line)
{
// Handle the line
}

See: http://us3.php.net/manual/en/function.file.php
What if the file is larger than memory_limit (8Mb)?

http://www.php.net/manual/en/ini.cor...i.memory-limit

fgets() is the right answer in this case.

So, instead of coding with file() which works now with a 200K file,
start with fgets and your script will not break when the file grows.

http://www.php.net/fgets

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Oct 13 '06 #5

P: n/a
I would go fgets, this will allow you to control how much of the file
you would like to do, or maybe possibly do it in batches.

On Oct 14, 4:17 am, Pedro Graca <hex...@dodgeit.comwrote:
[ Please don't top-post. Readhttp://www.caliburn.nl/topposting.html]

ZabMilenko top-posted (corrected):
howa wrote:
hi,
how to read a large php file line by line?
fread can't?
thanks.
$file_array = file($file_name);
foreach ($file_array as $line_number =$line)
{
// Handle the line
}
See: http://us3.php.net/manual/en/function.file.phpWhat if the file is larger than memory_limit (8Mb)?

http://www.php.net/manual/en/ini.cor...i.memory-limit

fgets() is the right answer in this case.

So, instead of coding with file() which works now with a 200K file,
start with fgets and your script will not break when the file grows.

http://www.php.net/fgets

--
File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot
Oct 15 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.