File Handling In PHP

Introduction

In this blog, I will demonstrate how to read, write, and delete a file using PHP web programming language. Before we create, read, write, and delete a file, we need to understand some PHP functions and file modes carefully. These functions and modes are described below.

File modes

File modes specify the access type of your file.

Mode Description
r Open a file for reading only. File pointer starts at the beginning of the file.
r+ Open a file for reading and writing. File pointer starts at the beginning of the file.
w Open a file for writing only. File pointer starts at the beginning of the file.
w+ Open a file for reading and writing. File pointer starts at the beginning of the file.
a Open a file for writing only. File pointer starts at the end of the file.
a+ Open a file for reading and writing. File pointer starts at the end of the file.

fopen(argu1,argu2) function

The fopen() function is used for opening a file. It takes two arguments - file path, and file mode. For example -

  1. $file_name = "D:/test.txt"// file path  
  2. $file = fopen($file_name"r");  

fwrite(argu1,argu2) function

The fwrite() function is used for writing a file. It takes two arguments - first, we need to specify a file pointer and second, specify our string that we want to write. For example -

  1. fwrite($file,"Hello world!");  

fread(argu1,argu2) function

The fread() function is used for reading a file. It takes two arguments - a file pointer, and the length of the file. For example -

  1. $size = filesize($file_name ); //return length of the file  
  2. $text = fread$file$size );  

Note
The filesize() function returns the size of a file in bytes.

fclose(argu1) function

The fclose() function is used for closing a file. It takes one argument that holds an opened file pointer. For example -

  1. fclose($file)   

unlink(argu1,argu2) function

The unlink() function is used for deleting a file. It takes two arguments - filename, and context. For example -

  1. unlink($file_name);  

Note
The second argument, context, is an optional parameter that can be used for modifying the nature of stream or file.

File Write

After opening a file using fopen() function, the file can be written using the fwrite() function.

Example 

  1. <html>  
  2. <head>  
  3. <title>write</title>  
  4. </head>  
  5. <body>  
  6. <?php   
  7. $file_name = "D:/test.txt"// file location path.  
  8. if(file_exists($file_name)) //file_exists() function check file does exist or not.  
  9.     {  
  10.         echo "File is already created....";  
  11.         exit();  
  12.     }  
  13. $file = fopen$file_name"w" ); //file opening in writing mode.  
  14. fwrite( $file"Hello world!" ); //file wrting   
  15. fclose( $file ); // fclose() function is used for closing a file.  
  16. echo"File created successfully...";  
  17. ?>  
  18. </body>  
  19. </html>  

File Read

After opening a file using the fopen() function, the file can be read using the fread() function.

Example

  1. <html>  
  2. <head>  
  3. <title>read</title>  
  4. </head>  
  5. <body>  
  6. <?php  
  7.     $file_name="D:/test.txt"// file location path.  
  8.     if(!file_exists($file_name)) //file_exists() function check file does exist or not.  
  9.     {  
  10.         echo "File not found....";  
  11.         exit();  
  12.     }  
  13.     $file=fopen($file_name,"r"); //file opening in reading mode.  
  14.     $size=filesize($file_name);  
  15.     $text=fread($file,$size); //file reading  
  16.     fclose($file); //fclose() function is used for closing a file.  
  17.       
  18.     echo "<h3><pre>$text</pre></h3>";  
  19. ?>  
  20. </body>  
  21. </html>  

File Delete

After opening a file using the fopen() function, the file can be deleted using the unlink() function.

Example

  1. <html>  
  2. <head>  
  3. <title>delete</title>  
  4. </head>  
  5. <body>  
  6. <?php  
  7.     $file_name="D:/test.txt"// file location path.  
  8.     if(!file_exists($file_name)) //file_exists() function check file does exist or not.  
  9.     {  
  10.         echo "File not found....";  
  11.         exit();  
  12.     }  
  13.     unlink($file_name); // file delete  
  14.     echo "File deleted...";  
  15. ?>  
  16. </body>  
  17. </html>  

File handling is an important part of any web programming language. In this blog, I covered file read, write, and delete functions using PHP language. Thanks for reading.

Next Recommended Reading JSON In PHP