Click here to Skip to main content
13,046,679 members (40,592 online)
Rate this:
Please Sign up or sign in to vote.
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


    * @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 =?> $line) {
    echo $line;

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


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
Web02 | 2.8.170713.1 | Last Updated 29 Apr 2013
Copyright © CodeProject, 1999-2017
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