PHP File Handling How to Read, Write, and Manipulate Files

admin_img Posted By Bajarangi soft , Posted On 07-10-2022

PHP supports file handling which is used to read, write and append data to the file.

File Handling In PHP

PHP File() Handling & Functions

File handling is needed for any application. For some tasks to be done file needs to be processed. File handling in PHP is similar as file handling is done by using any programming language like C.

What is a File?

A file is simply a resource for storing information on a computer.

Files are usually used to store information such as:

  • Configuration settings of a program
  • Simple data such as contact names against the phone numbers.
  • Images, Pictures, Photos, etc.

PHP File Formats Support

  • File.txt
  • File.log
  • File.custom_extension i.e. file.xyz
  • File.csv
  • File.gif, file.jpg etc
  • Files provide a permanent cost effective data storage solution for simple data compared to databases that require other software and skills to manage DBMS systems.
  • You want to store simple data such as server logs for later retrieval and analysis
  • You want to store program settings i.e. program.ini

File Handling Operations

  1. Create a Filefopen()
  2. Open a Filefopen()
  3. Read a Filefread()
  4. Write to a Filefwrite()
  5. Append to a Filefwrite()
  6. Close a Filefclose()
  7. Delete a Fileunlink()
      in this functions just by changing one or more arguments, same function can be used to perform multiple operations on file.

Files can be opened in any of the following modes :

<?php
$file = fopen(“demo.txt”,'w');
?>
  • “w” – Opens a file for write only. If file not exist then new file is created and if file already exists then contents of file is erased.
  • “r” – File is opened for read only.
  • “a” – File is opened for write only. File pointer points to end of file. Existing data in file is preserved.
  • “w+” – Opens file for read and write. If file not exist then new file is created and if file already exists then contents of file is erased.
  • “r+” – File is opened for read/write.
  • “a+” – File is opened for write/read. File pointer points to end of file. Existing data in file is preserved. If file is not there then new file is created.
  • “x” – New file is created for write only.

Reading a file :

Once a file is opened using fopen() function it can be read with a function called fread(). This function requires two arguments. These must be the file pointer and the length of the file expressed in bytes.

The files length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.

So here are the steps required to read a file with PHP.

  • Open a file using fopen() function.
  • Get the file's length using filesize() function.
  • Read the file's content using fread() function.
  • Close the file with fclose() function.
<?php
$filename = "demo.txt";
$file = fopen( $filename, 'r' );
$size = filesize( $filename );
$filedata = fread( $file, $size );
?>

Writing a file

A new file can be written or text can be appended to an existing file using the PHP fwrite() function. This function requires two arguments specifying a file pointer and the string of data that is to be written. Optionally a third integer argument can be included to specify the length of the data to write. If the third argument is included, writing would will stop after the specified length has been reached.
 
<?php
$file = fopen("demo.txt", 'w');
$text = "Hello world\n";
fwrite($file, $text);
?>

Closing a file

<?php
$file = fopen("demo.txt", 'r');
//some code to be executed
fclose($file);
?>
 

Related Post