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 !

21 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.

Unknown said...

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

Unknown 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

nilashri 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

Unknown 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

Unknown 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

Unknown said...

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post I would like to read this
Data Science course in kalyan nagar | Data Science course in OMR
Data Science course in chennai | Data science course in velachery
Data science online course | Data science course in jaya nagar

shethal said...

This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
Devops training in sholinganallur
Devops training in velachery
Devops training in annanagar
Devops training in tambaram

Anonymous said...

This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 

angularjs-Training in tambaram

angularjs-Training in sholinganallur

angularjs-Training in velachery

angularjs Training in bangalore

angularjs Training in bangalore

angularjs Training in btm

rohini said...

Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
apple service center | apple iphone service center | apple ipad service center | apple mac service center

Imran said...

verdict blog
SAP Training in Chennai
SAP ABAP Training in Chennai
SAP Basis Training in Chennai
SAP FICO Training in Chennai
SAP MM Training in Chennai
SAP PM Training in Chennai
SAP PP Training in Chennai
SAP SD Training in Chennai

Softgen Infotech said...

Post is very useful. Thank you, this useful information.

Learn Best Tableau Training in Bangalore from Experts. Softgen Infotech offers the Best Tableau Training in Bangalore.100% Placement Assistance, Live Classroom Sessions, Only Technical Profiles, 24x7 Lab Infrastructure Support.

shree said...

very nice....
inplant training in chennai for it
namibia web hosting
norway web hosting
rwanda web hosting
spain hosting
turkey web hosting
venezuela hosting
vietnam shared web hosting

shri said...

very good post...!
internship in chennai for ece students
internships in chennai for cse students 2019
Inplant training in chennai
internship for eee students
free internship in chennai
eee internship in chennai
internship for ece students in chennai
inplant training in bangalore for cse
inplant training in bangalore
ccna training in chennai


Aadhya said...

Myself so glad to establish your blog entry since it's actually quite instructive. If it's not too much trouble continue composing this sort of web journal and I normally visit this blog. Examine my administrations.  
Read these Salesforce Admin Certification Topics which are really helpful. I read these Salesforce Admin and Developer Certification Dumps and very much useful for me. I recommend this Salesforce Developer Training and Certification Course for you.  

Nick569 said...

Great post! I am actually getting ready to across this information, It’s very helpful for this blog. Also great with all of the valuable information you have Keep up the good work you are doing well.
CRS Info Solutions Salesforce training for beginners 

Devi said...

Infycle Technologies, the best software training institute in Chennai offers the best Oracle training in Chennai for students, freshers, and tech professionals. In addition to that, other in-demand courses such as Big Data, Java, Python, Power BI, Digital Marketing will be trained with 200% practical classes. Once the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7502633633 to get more info and a free demo. Best Oracle Training in Chennai | Infycle Technologies

INFYCLE TECHNOLOGIES said...

Grab the Digital Marketing Training in Chennai from Infycle Technologies, the best software training institute, and Placement center in Chennai which is providing professional software courses such as Data Science, Artificial Intelligence, Cyber Security, Big Data, Java, Hadoop, Selenium, Android, and iOS Development, DevOps, Oracle, etc with 100% hands-on practical training. Dial 7502633633 to get more info and a free demo and to grab the certification for having a peak rise in your career.