PHP fget: read a file line by line

To get only one line of a text file, or get line by line, we have to use fget() php function instead of fread() .

We saw in the previous lesson, that if you want to read a file, you can open it and put all it's content in some variable. But in this script we will see how to get the data from the file line by line until the end of file is reached:
<?php

$filename = 'myfile.txt' ;
$file = fopen($filename , 'r' ) or exit ( "Enable to open file");

while(!feof($file))
{
echo fgets($file) . "<br />" ;
}

?>

Why to use this script:
Let's say you have a text file with many line:

----myfile.txt------
Hello Word.
We love PHP !
Here are great tutorials
And scripts and codes !
--------------------

if you use this script
$contents = fread($handle, filesize($filename));
echo $contents;
This will show:
Hello Word.We love PHP !Here are great tutorialsAnd scripts and codes !
And this is beacuse HTML script don't understand the new line character, so you need to create a new line with "<br />".

So we have to use:

<?php

$filename = 'myfile.txt' ;
$file = fopen($filename , 'r' ) or exit ( "Enable to open file");

while(!feof($file))
{
echo fgets($file) . "<br />" ;
}

?>
Display:
Hello Word.
We love PHP !
Here are great tutorials
And scripts and codes !

15 comments:

Amine said...

Thank you;
I look for this information and in forums i fund only questions!!!
really...thanks.

Diablo 3 Items said...

Cute tale. Helps make me personally wish to be much better. Thanks for sharing. You might be fantastic.
sell d3 gold



Diablo 3 gold

Guild wars 2 gold said...

wonderful submit... will help me a good deal,it is Exactly what I was trying to find! diablo 3 gold, i like it very much.

mary Brown said...

I have read your blog its very attractive and impressive. I like it your blog.

PHP Training in Chennai | Online PHP Training

PHP Training in Chennai | Online PHP Training

caroline jesi said...

These all are notable facts… I stick with responsive website design for my business. With the mobile marketing is at its peak stage, having website that goes comfy on all devices will help your business. You can check my website for more details on the importance of responsive website design. PHP Training Institute in Chennai | Best PHP training in Chennai | PHP Training Institute in Chennai

JamesWilliam said...

Informative blog and it was up to the point describing the information very effectively. Thanks to blog author for wonderful and informative post...
Android application developers Chennai | Android application developers India

kanaga seo said...

Nice article Thanks for sharing this article Wonderful information from this website Thanks !!!
Embedded training in chennai | Embedded training centre in chennai

aruna raj said...

Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
MEAN stack training in Chennai
MEAN stack training in bangalore

Nila shri said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
Data science training in Bangalore

Data science training in tambaram

Data science training in kalyan nagar

Data Science training in OMR

Data Science training in anna nagar

Data Science training in chennai

Data Science training in marathahalli

Data Science training in BTM layout

Data Science training in rajaji nagar

shalinipriya said...

Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 
Devops training in Chennai
Devops training in Bangalore
Devops Online training
Devops training in Pune

amilu B said...

After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
java training in chennai | java training in bangalore

java online training | java training in pune

selenium training in chennai

selenium training in bangalore

johnsy sai said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
Devops Training in pune

Devops Training in Chennai

Devops Training in Bangalore

AWS Training in chennai

AWS Training in bangalore

digi mark said...

Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.

rpa training in Chennai | rpa training in pune

rpa training in tambaram | rpa training in sholinganallur

rpa training in Chennai | rpa training in velachery

rpa online training | rpa training in bangalore

john jersy said...

This is beyond doubt a blog significant to follow. You’ve dig up a great deal to say about this topic, and so much awareness. I believe that you recognize how to construct people pay attention to what you have to pronounce, particularly with a concern that’s so vital. I am pleased to suggest this blog.
python training in chennai
python training in chennai
python training in Bangalore

aamir khan said...

I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.
java training in chennai | java training in bangalore


java training in tambaram | java training in velachery