file handling in php

What is File Handling in php -: A file represents in a sequence of bytes on the storage disk where a group of related data’s are  stored. File is created for permanent storage of data. php provide a number of functions that helps to perform basic file operations. These are the function  in php use the file handle as like open a file,close a file, read a file in php etc.

In other words we can say that the file handling means in php full of control of a file.using these function we can create a file, edit a file ,delete a file , save a file for future ,read and write in the file.File handling function provide many facility in php. The function  are different  like as a

 How to open a file in php –: When we want to open a file in php we can use this fopen() function in php. For example

<?php

$file = fopen(“demo.txt”,'r'); 

?>

In this example $file is the name of the variable name and demo.txt is the file name which we want to open it. In this we see “r” it means we can read only this file and we can not edit it.It’s also provide some other function facility for read,write and more facility. these are the

  • ‘r’ -: This function provide open a file read only.
  • ‘w’ -:This function provide open a file write only.File pointer starts at the beginning of the file
  • ‘a’-:This function provide open a file write only. File pointer starts at the end of the file. Creates a new file if the file doesn’t exist
  • ‘x’-: This function provide open a file create a write file only.Returns FALSE and an error if file already exists
  • ‘r+’: This function provide open read and write only.File pointer starts at the beginning of the file
  • ‘w+’ -:This function provide open read and write. Erases the contents of the file or creates a new file if it doesn’t exist. File pointer starts at the beginning of the file
  • ‘a+’ -: This function provide open a file read and write.File pointer starts at the end of the file. Creates a new file if the file doesn’t exist
  • ‘x+’ -: This function provide open a new file read and write only.

 

How to read a file in php-: For read a file only we can use fread() function in php as like

echo fileread("filename.txt");

Close a file in php-: For closing a file only we can use fclose() function in php.

<?php

  $file = fopen(“demo.txt”,'r');

//Some code write here

  fclose($file);

 ?>

php read single line -: For reading a single line we can use fgets() function in php. For example..

<?php

$file = fopen(“demo.txt”,'r');

echo fgets($file);

fclose($file);

?>

php check end of the file –: For checking the end of the file we use feof() function in php as like

<?php

$file = fopen(“demo.txt”,'r');

while(!feof($file); {

echo fgets($file);
                    }


?>

 

Read single character in php -: for reading a single character we can use fgetc() function in php. For example

<?php

$file = fopen(“demo.txt”,'r');

while(!feof($file); {

echo fgets($file);
}

fclose($file);

?>

 

 

Leave a Reply

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