Click here to Skip to main content
11,642,855 members (71,026 online)
Rate this: bad
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

     * @author gencyolcu
     * @copyright 2013
    $file=fopen("m.txt","r+") or exit("couldn't open file");
        echo fgets($file). "<br />";
Posted 28-Apr-13 16:44pm

1 solution

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

Solution 1


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 />";
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
$file1 = "G:\\Events.txt";
$lines = file($file1);
foreach($lines as $line_num =?>
<?php $strFile = "G:\\Events.txt";
$hFile = fopen($strFile, "r");
while (($line = fgets($hFile)) !== false) {
    echo $line;

TheSniper105 at 29-Apr-13 0:49am
is there any way to check end of the line not file??
TheSniper105 at 29-Apr-13 1:50am
could u please format ur code can't read it
Prasad Khandekar at 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
0 DamithSL 328
1 OriginalGriff 280
2 Sergey Alexandrovich Kryukov 270
3 Afzaal Ahmad Zeeshan 195
4 190
0 DamithSL 541
1 OriginalGriff 495
2 Mika Wendelius 469
3 Sergey Alexandrovich Kryukov 330
4 Afzaal Ahmad Zeeshan 324

Advertise | Privacy | Mobile
Web04 | 2.8.150731.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2015
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