?
Solved

php Read each line until end of file

Posted on 2009-05-07
3
Medium Priority
?
2,293 Views
Last Modified: 2013-12-12
Im trying to write a php script to read each line of a file, and continue looping until the end of the file is reached, but cannot find out how to detect the end of the file.

Code in vb.net would be:-
        FileOpen(1, "c:\temp.txt", OpenMode.Input)
        While EOF(1) = False
            inputStr = LineInput(1)
        End While

Only code I can really find at the moment, reads the top 3 lines of the file (I know I can repeat it xx times, but the file can be different lengths.

Any help would be appriciated.

Thanks in advance
$myFile = "C:\\phpTest\\testFile.txt";
$fh = fopen($myFile, 'r');
 
$theData = fgets($fh);
echo $theData;
 
$theData = fgets($fh);
echo $theData;
 
$theData = fgets($fh);
echo $theData;

Open in new window

0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Accepted Solution

by:
Veonik earned 1000 total points
ID: 24329404
I would use file_get_contents()

Otherwise, use a while($theData = fgets($fh)) loop
// Get the whole file into a single string
$fileContents = file_get_contents("C:\\phpTest\\testFile.txt");
 
// Explode the file contents string by each line
$lines = explode(PHP_EOL, $fileContents); // Replace PHP_EOL with "\r\n" or "\n" or "\r" if you like
 
// Iterate through each line and do what you need with it
foreach ($lines as $line) {
   echo $line;
}

Open in new window

0
 
LVL 4

Assisted Solution

by:bljak
bljak earned 1000 total points
ID: 24333406
Veonik is right, but fgets solution looks like in the code snippet.
feof() is your friend.
$myFile = "C:\\phpTest\\testFile.txt";
$fh = fopen($myFile, "r");
 
if ( $fh )
{
  while ( !feof($fh) )
  {
    $line = fgets($fh);
  }
  fclose($fh);
}

Open in new window

0

Featured Post

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question