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 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 paramiters 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 is lines on the file and the function to detect the end of 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 files lines, if there is not end of 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 cloased 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 is 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 paramiters 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 writteing.
  • 2. Then write the text on the file what you want, and the function for that is fwrite(); and through the function have to pass 2 peramiter and they are i. the first one is the opened file and the seconf one is ii. the text you want to write on the opened file.
  • 3. And after printing or working with the file have to cloased 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);
?>

Appned on a File


The appned process is similar to the file writting process of php and it also have 3 steps similar to the file writting and the only different is have to opene the file in appned mode and for that have to send the second peremiter as ‘a’ as a denote for file appneding.

The 3 steps are to Appned on a HPH File

  • 1. First open the file in appned mode and the function name to open the file is fopen(); and there have to send 2 paramiters 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 appneding.
  • 2. Then write the text on the file what you want, and the function for that is fwrite(); and through the function have to pass 2 peramiter and they are i. the first one is the opened file and the seconf one is ii. the text you want to write on the opened file.
  • 3. And after printing or working with the file have to cloased the opened file and the function is fclose(); and have to pass the opened file through the function.

Example of PHP Appned 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 server the function is unlink(); and have to send the file path through the function and its better to detect if the file is exist, els the function will return error.

Example of Delete a File in PHP:

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

I hope the post have 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 -

Add a Comment

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.