Click here to Skip to main content
15,906,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
<?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

1 solution

Hello,

You can feof function to check the end of file. Please see the modified code below.
PHP
$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.
PHP
You could try the following way
<?php
$file1 = "G:\\Events.txt";
$lines = file($file1);
foreach($lines as $line_num =?> $line) {
    echo $line;
}
?>

OR
PHP
<?php $strFile = "G:\\Events.txt";
$hFile = fopen($strFile, "r");
while (($line = fgets($hFile)) !== false) {
    echo $line;
}
?>

Regards,

Regards,
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900