PHP File Handling Complete Tutorial with working example

PHP File Handling Complete Tutorial with working example

PHP File Handling Tutorial –  File is collections of characters, words, lines, paragraphs and pages from a textual document
A text file can be a stream of characters that a computer can process sequentially. It is not only processed sequentially but only in forward direction. For this reason a text file is usually opened for only one kind of operation (reading, writing, or appending) at any given time.  In PHP a file is created using touch() function.
Example:-
pro1.php
<?php
touch(“abc.txt”);
echo “File created”;
?>
In the example  abc.txt file create. If you want to create a file in particular directory, so  mention
It’s path. As  for example ,if you want to  create file  in data directory which is located in your project directory.
Example:-
pro2.php
<?php
touch(“data/abc.txt”);
echo “File created”;
?>
Now we will try to write some data on existing file. So first of all open the file . in PHP fopen() function is used to open a file. It takes two parameter  file name and purpose to open.
Syntax-foen(“file name”,”mode (read/write/append)”)
Second parameter is also called mode.
Mode    Definition
————————
r-             read
w-           write
a-            append
now we want to write some thing on abc.txt.  so  open file in write mode . foen(“data/abc.txt”,’w’) function  open the file in write mode and return  address where file is opened  in  memory. Now all process will complete on that pointer variable. Try to understand the following example.
pro3.php
<?php
$fp=fopen(“data/”.”abc.txt”,’w’);
fwrite($fp,”welcome”);
echo “Data inserted”;
?>

Reading data from a file . fread() function is used for reading data from file, it takes two parameter as file handler and  size(in character).

pro4.php
<?php
$fp=fopen(“data/abc.txt”,’r’);
$data=fread($fp,20);
echo $data;
?>
In the above example we have pass 20 in fread () function as second argument. It maean fread will read only 20 character from $fp handler. If we want to read complete file then how it possible.
Following example illustrate how a complete will read. For reading the complete file we have need  size of complete file. filesize() is a function which  takes file name as a parameter and return  file size in character.
Example:-
pro5.php
<?php
$fp=fopen(“data/abc.txt”,’r’);
$size=filesize(“data/abc.txt”);
$data=fread($fp,$size);
echo $data;
?>

fgetc()-  is a function which return single character from handler and place the file pointer to next .

Example:-
pro6.php
<?php
$fp=fopen(“data/abc.txt”,’r’);
$data=fgetc($fp);
echo $data;
?

feof()- is a function which  takes file handler as a parameter and return true if the control reach at the end of file.

Example:-
pro7.php
<?php
$fp=fopen(“data/abc.txt”,’r’);
if(!feof($fp))
echo”True”;
else
echo”False”;
?>

PHP reading a file character by character  using fgetc() and feof() function.

pro8.php
<?php
$fp=fopen(“data/abc.txt”,’r’);
while(!feof($fp))
{
$data=fgetc($fp);
echo $data;
}
?>

PHP Reading a file  line by line using fgets() function

pro9.php
<?php
$fp=fopen(“data/abc.txt”,’r’);
while(!feof($fp))
{
$data=fgets($fp);
echo $data;
}
?>

  ftell():- function return the position of pointer in file

pro10.php
<?php
$fp=fopen(“data/abc.txt”,’r’);
while(!feof($fp))
{
$data=fgetc($fp);
echo $data.”=”.ftell($fp).”<br>”;
}
?>

PHP Appending  data in a file

pro11.php
<?php
$fp=fopen(“data/”.”abc.txt”,’a’);
fwrite($fp,”welcome”);
echo “Data Updated”;
?>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Do you have any Query / Suggestion