News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

PHPro.org:
Read Line From File (stream_get_line)
April 23, 2009 @ 09:36:51

The PHPro.org website has a quick new tutorial about a method (using streams) to get the information from a certain line of a file.

Reading files in PHP can be a tricky business if not handled correctly. Most often when confronted with reading a line from, the nearest tool to hand is the file() function. The problem with using the file() function is that it reads the whole file into an array, and thus, into memory [...] A better, and more efficient way is to loop through the file stream using the stream_get_line() function. Care still needs to be taken to clear the buffer on each iteration, or the same problem could potentially arise as with the file() method.

The code calls fopen on the file and, while it's not the end of the file, uses the stream_get_line function to grab things a line at a time. This saves you from having to read in the entire file (like with a file_get_contents or file - especially good for large files.

0 comments voice your opinion now!
stream tutorial file line read streamgetline


blog comments powered by Disqus

Similar Posts

SitePoint PHP Blog: Lightning-fast WordPress with PHP-FPM and nginx

Web Development Blog: PHP Mail Scripts using SMTP transport, a guide for beginners

PseudoCoder.com: How To Really Use Xdebug To Speed Up Your App

Zend Developer Zone: Using Zend_Feed to Merge Multiple RSS Feeds

ServerGrove Blog: Spooling emails with Symfony2 on VPS and Shared Hosting


Community Events





Don't see your event here?
Let us know!


language voicesoftheelephpant symfony mvc update introduction community security package laravel library series opinion framework release tool version composer interview podcast

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework