Click here to Skip to main content
12,067,351 members (55,313 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: PHP
i want to check if the end of line is reached but when i run

the code it doesn't show anything although the file has contents
my code is as following


<?php
 
   /**
    * @author gencyolcu
    * @copyright 2013
    */
 
   $file=fopen("m.txt","r+") or exit("couldn't open file");
 
   while(!PHP_EOL)
   {
       echo fgets($file). "<br />";
   }
 

   fclose($file);
 
   ?>
Posted 28-Apr-13 17:44pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hello,

You can feof function to check the end of file. Please see the modified code below.
$file = fopen("m.txt","r+") or exit("couldn't open file");
while(!feof($file) {
    echo fgets($file) . "<br />";
}
fclose($file);
For fgets function if you do not specify the length then it will keep reading from the stream until it reaches the end of the line.

To read a file line by line see code below.
You could try the following way
<?php
$file1 = "G:\\Events.txt";
$lines = file($file1);
foreach($lines as $line_num =?>
OR
<?php $strFile = "G:\\Events.txt";
$hFile = fopen($strFile, "r");
while (($line = fgets($hFile)) !== false) {
    echo $line;
}
?>
Regards,

Regards,
  Permalink  
v2
Comments
TheSniper105 29-Apr-13 0:49am
   
is there any way to check end of the line not file??
TheSniper105 29-Apr-13 1:50am
   
could u please format ur code can't read it
Prasad Khandekar 29-Apr-13 1:44am
   
I have updated the solution, it now includes the code for reading line by line as well.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160208.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100