Need help in reading text file

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
rajsekar2u
Forum Commoner
Posts: 71
Joined: Thu Nov 20, 2008 4:23 am

Need help in reading text file

Post by rajsekar2u »

Hi,

I have a doubt in reading text file.
I've detailed the issue below.

I need to read a text file of 450MB and need to display a row in it.
Is there any way to reduce the reading time. because its showing me the below error.

Code: Select all

Fatal error: Allowed memory size of 373293056 bytes exhausted (tried to allocate 78 bytes) in E:\xampp\htdocs\projects\test\car\test.php  on line 11
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Need help in reading text file

Post by Jonah Bron »

Code: Select all

function get_line($file, $line_to_read) {
    $file = fopen($file, 'r');
    while ($line_to_read > 0) {
        fgets($file, 0);
        $line_to_read--;
    }
    return fgets($file);
}
echo get_line('file.txt', 42);
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Need help in reading text file

Post by AbraCadaver »

How do you know what row? Is it on a specific line or do you need to search for something in order to find it?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
lcarron000
Forum Newbie
Posts: 13
Joined: Thu May 20, 2010 2:51 pm

Re: Need help in reading text file

Post by lcarron000 »

I don't know how much memory your server has but, try this ...

Code: Select all

ini_set("memory_limit","500M");
Post Reply