PHP File Read, Write, Append and Delete

PHP File Read, Write, Append and Delete

PHP File Read, Write, Append and Delete


In our previous post, we have learned how to upload files in PHP, in this post we are going to learn Read, Write, Append and Delete PHP file. In the case of PHP file function there is mainly 4 process and they are

  • 1. Read File

  • 2. Write File

  • 3. Append File

  • 4. and Delete File

now we are going to discuss the 4 functions and its working process of PHP file step by step.

Read File


to read a file in PHP and print in a page the steps are as described below,

  • 1. First open the file in read mode and the function name to open the file is fopen(), and there have to send 2 parameters and the first one is the path of the file and the second one is for what, and its PHP pree defined here it should be ‘r’ as ‘r’ denote for file reading.
  • 2. Detect if there are lines on the file and the function to detect the end of the line on the file is feof(), and inside that have to pass the opened file. It should take in a while to continue the process.
  • 3. Print the lines of the file, if there is no end of the line of the file and the function to print the lines is fgets(); and have to pass the opened file through the function
  • 4. and after printing or working with all the lines have to close the opened file and the function is fclose(), and have to pass the opened file through the function.

Example of PHP Read File:

<?php
$file = fopen("path of file", "r");
while(!feof($file))
  {
  echo fgets($file). "<br>";
  }
fclose($file);
?>

Write on a File


To write in a file there are 3 steps and they are as described below,

  • 1. First open the file in write mode and the function name to open the file is fopen(), and there have to send 2 parameters and the first one is the path of the file and the second one is for what, and its PHP pree defined here it should be ‘w’ as w denote for file writing.
  • 2. Then write the text on the file that you want, and the function for that is fwrite(), and through the function have to pass 2 parameters and they are
    • i. the first one is the opened file and the second one is
    • ii. the text you want to write on the opened file.
  • 3. And after printing or working with the file have to close the opened file and the function is fclose(), and have to pass the opened file through the function.

Example of PHP Write-On File::

<?php
$file = fopen("path of file", 'w');
fwrite($file, "File contents to be written on the file.....");
fclose($file);
?>

Append on a File


The append process is similar to the file writing process of PHP and it also has 3 steps similar to the file writing and the only difference is to have to opene the file in append mode and for that have to send the second perimeter as ‘a’ as a denote for file appending.

The 3 steps are to Append on an HPH File

  • 1. First open the file in append mode and the function name to open the file is fopen(), and there have to send 2 parameters and the first one is the path of the file and the second one is for what, and its PHP pree defined here it should be ‘a’ as a denote for file appending.
  • 2. Then write the text on the file that you want, and the function for that is fwrite(), and through the function have to pass 2 parameters and they are i. the first one is the opened file and the second one is ii. the text you want to write on the opened file.
  • 3. And after printing or working with the file have to close the opened file and the function is fclose(), and have to pass the opened file through the function.

Example of PHP Append on a File:

<?php
$file = fopen("path of file", 'a');
fwrite($file, "File contents to be appened or add more on the file.....");
fclose($file);
?>

Delete a File


To delete a file from the server the function is unlink(), and have to send the file path through the function and its better to detect if the file exists, else the function will return an error.

Example of Delete a File in PHP:

<?php
if(file_exists(path of file)){
	unlink("path of file");
}
?>

I hope the post has helped you to learn PHP File Read, Write, Append and Delete.

Thanks for Reading.

Please Give your valuable comment to make the site more useful

Share The Post On -