pTutorial

New way to learn PHP

PHP 5 File handling

File handling

Sometimes application require the store the data for very long time. You can store data for fixed time interval by using cookies or session. Cookies can be deleted by the user at any time. To be available and stable, the information needs to be stored somewhere secure, where no one can access or modify. The information needs to be stored on the server.


There are two ways to store data on server.

  1. File
  2. Database

File:-Files are text files that are human readable and easily modifiable.

Database:-I have already explain concept of database in previous section.


Advantages of file over databases

Available and versatile: You can easily create and save data in file at any operating system without installing any extra software.

Easy to use: You can easily read or write the data of file without do any extra thing like creating, connecting database and without extra software.

Smaller: files takes very less space as compare to database.

.

At the end you can say that files are good for small amount of data.

Example of reading file data

<?php

if(file_exists('test.txt'))
{
echo "File found<br>";
$fh = fopen("demo.txt","r") ;
while(!feof($fh))
{
$line = fgets($fh);
echo $line."<br/>";

}
}
else
{
echo "File not found";	
	}

?>

Output

At the end you can say that files are good for small amount of data.

All the componect that are used in this program will explain in the next sections.


QUOTES OF THE DAY

Each day provides its own gifts.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER