communication
 databases
 directories
 file handling
 html and code
 image editing
 image listing
 sessions and co
 string handling
 information
 forms
 apple stuff
 image handling
 array handling
 lixlpixel
php - snippets
google search terms detected and highlighted.
remove highlighting | keep highlighting
make snippet in "file handling" - download "line by line"
 search
php - snippets

Valid CSS!

php webring logo
prev next
random list
join the Ring!

file handling - line by line was created/last modified on Mon 22 Mar 2004 1:04 PM

a rather lenghty solution for
   
   <?php
   
   file
('path/to/file.txt');
   
   
?>
   
but well, here you go ...

   <?php
     
    
// GetLine
    // Obtain the next line in a given file by reading each character until a \r or \n is reached.
     
    // @param    file    a handle returned from fopen for our file.
    // @return    string    the next line in the file.
     
     
function getLine($file)
     {
     
        
// iterate over each character in line.
        
while (!feof($file))
        {    
     
            
// append the character to the buffer.
            
$character = fgetc($file);
            
$buffer .= $character;
        
            
// check for end of line.
            
if (($character == "\n") or ($character == "\r"))
            {
     
                
// checks if the next character is part of the line ending, as in
                // the case of windows '\r\n' files, or not as in the case of
                // mac classic '\r', and unix/os x '\n' files.
                
$character = fgetc($file);
                if (
$character == "\n")
                {
                
                    
// part of line ending, append to buffer.
                    
$buffer .= $character;
                
                } else {
                
                    
// not part of line ending, roll back file pointer.
                    
fseek($file, -1, SEEK_CUR);
                }
            
                
// end of line, so stop reading.
                
break;
            }
        }
        
// return the line buffer.
        
return $buffer;
     }
     
     
?>

learn more about some of the PHP functions used in this snippet: break, case, dl, each, else, end, feof, fgetc, file, fopen, for, fseek, function, next, return, while

view the GNU general license - view the GNU library license - search on PHPsnippets - see your code like this

 a lixlpixel.com site

PHParadise.com - PHP-classes.org - your site on safari