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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?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; ?> |