469,366 Members | 2,244 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

PHP read large file line by line?

hi,

how to read a large php file line by line?

fread can't?

thanks.

Oct 13 '06 #1
5 72346
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

$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
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
[ 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
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.

Similar topics

8 posts views Thread by Chris | last post: by
2 posts views Thread by ohaya | last post: by
6 posts views Thread by guillaume | last post: by
1 post views Thread by Yong Wang | last post: by
12 posts views Thread by Martin Dieringer | last post: by
2 posts views Thread by Kevin Ar18 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.