PHP 7 File Handling Script to Open,Read and Write Files on Server in Browser Full Project For Beginners

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

 

Welcome folks today in this blog post we will be opening,reading and writing files on server using file handling in browser on server using php 7. All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started you need to make an index.php file and copy paste the following code

 

 

First we will be opening and reading files

 

 

index.php

 

 

<?php

$file = "myfile.txt";

//opening a file in reading mode
$h = fopen($file, 'r');

//reading file upto file length and storing it in a variable "fcontents"
$fContents = fread($h, filesize($file));

//closing file
fclose($h);

//displaying the content
echo $fContents;

?>

 

 

 

Now we will be writing files

 

 

index.php

 

 

<?php

$file = "myfile.txt";

//opening a file in writing mode
$h = fopen($file, 'a');

//adding new contents to file
$ncontents = "This line added to the file.";
fwrite($h, $ncontents);

//closing file
fclose($h);

//opening a file in reading mode
$h = fopen($file, 'r');

//reading file to check whether content is added or not
$fContents = fread($h, filesize($file));

//closing file
fclose($h);

//displaying the content
echo $fContents;

?>

Leave a Reply