Pages

Subscribe:

Ads 468x60px

Sunday, July 25, 2010

How to Read and Show Text File Contents in PHP?

When you work with dynamic website you have to use database and tables. You insert data into a table of a database and read data from that particular table when needed. Suppose, you create a table and insert a long description about your webpage in that table. But it can easily be done creating a text file into your computer and then just calling this file using PHP code.


In the later case no database and table is needed. So your page will be loaded fast and help people who use slow connection internet i.e. 3rd world country people. In the following, I am inserting my code and describing so that you can easily understand.

1st Part:
<?php $fp = fopen("files/abc.txt", 'r');
print "<marquee direction=left scrolldelay=2 onmouseover=stop() onmouseout=start() scrollamount=3 onMouseOver=exit>"
?>
[Here abc.txt is a text file that resides under folder "files" in root directory. Text file is read through this command. Marquee direction makes the total file contents moving from right to left and the mouse pointer will be taken upon that line rotation will stop. When mouse pointer will be removed, moving will start again.]

2nd Part:
<?php
while (!feof($fp))
{
$line = fgets($fp, 1024);
echo "$line";
}
?>
[A loop goes here from beginning to the end of that file and read every lines one by one.]

3rd Part:
<?php
fclose($fp);
?>
[Here by fclose command, the existing file is closed.]

0 comments:

Post a Comment