Click here to Skip to main content
11,496,146 members (469 online)
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
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 17:44pm

1 solution


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 Sergey Alexandrovich Kryukov 10,401
1 OriginalGriff 8,910
2 Sascha Lefèvre 3,899
3 Maciej Los 3,422
4 Richard Deeming 2,600

Advertise | Privacy | Mobile
Web04 | 2.8.150520.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