X

Rate Us :



Share with Friends :

Menu

PHP-File Handling :

File Handling is the concept of reading the file contents as well as writing the file contents. PHP provides number of functions to read and write the file contents. If we want to read and write the contents of file, first we need to open the file with the specific file mode.

Different types of file modes are available :-

r(read) :

To read the file contents. If we open any file with read mode, file pointer locates at the beginning of file.


w(write) :

To write the contents in a file. If we open any file with write mode it deletes the previous contents of files & locates the file pointer at starting position. If the file is not available, it creates a new file.


a(append) :

To append some text with the existing content of file. The file pointer locates at the end of the file.


r+(read/write) :

To read and write the contents of a file. It is same as read mode but we can also write file contents.


w+(write/read) :

It is same as write mode but we can also read file contents.


a+(append/read) :

It is same as append mode but we can also read file contents.


Fopen() :

By using this function, we can open a file with specific file mode. It contains two arguments - File name & File mode.


Fread() :

To read the file contents, arguments or file pointer and file size.


Fwrite() :

To write the contents in a file, arguments or file pointer and new content.


Filesize() :

To get the total number of bytes occupied by a file. Argument is File name.

Example 1 :

<?php $fp = Fopen("myfile.txt", "r"); $size = Filesize("myfile.txt"); $cont = Fread($fp, $size); echo $cont; ?>


Example 2 :

<?php $fp = Fopen("myfile.txt", "r+"); Fwrite($fp, "Man"); echo Fread($fp, Filesize("myfile.txt")); ?>


Example 3 :

<?php $fp = Fopen("myfile.txt", "w"); Fwrite($fp, "NY"); ?>


Example4 :

<?php $fp = Fopen("myfile.txt", "a"); Fwrite($fp, "NY"); ?>


rewind() :

By using this function, we can locate the file pointer at the starting location.

Example :

<?php $fp = Fopen("myfile.txt", "r+"); Fwrite($fp, "Man"); echo Fread($fp, Filesize("myfile.txt")); ?>


Fseek() :

By using this function, we can locate the file pointer on specified location.

Example :

<?php $fp = Fopen("myfile.txt", "w+"); Fwrite($fp, "Man"); Fseek($fp, 2); echo Fread($fp, Filesize("myfile.txt")); ?>


Fgets() :

By using this function, we can read a line from a string & it locates the file pointer at the beginning of next line.

Example :

<?php $fp = Fopen("myfile.txt", "r"); $line = Fgets($fp); echo $line; $line = Fgets($fp); echo $line; ?>


Fgetss() :

It is same as Fgets(), but it ignores the html elements.


Fgetc() :

By using this function, we can get a character from a file.

Example :

<?php $fp = Fopen("myfile.txt", "r"); $line = Fgetc($fp); echo $line; $line = Fgetc($fp); echo $line; ?>


File_get_contents() :

By using this function, we can read the contents of a file without file pointer.

Example :

<?php echo File_get_contents("myfile.txt"); ?>


File_put_contents() :

By using this function, we can write new contents in a file.

Example :

<?php echo File_put_contents("myfile.txt"); ?>


readFile() :

By using this function, we can read the contents of a file at the same time we can write those contents in current buffer location.

Example :

<?php readFile("myfile.txt"); ?>


File() :

By using this function, we can read each & every line of a file & returns every line as array elements.

Example :

<?php $arr = File("myfile.txt"); print_r($arr); ?>


Filectime() :

By using this function, we can get the created date & time information of a file. It returns output as timestamp value.

Example :

<?php echo date('d/m/y', Filectime("myfile.txt")); ?>


Fileatime() :

By using this function, we can get the last access date & time information of a file.


Filemtime() :

By using this function, we can get the last modified date & time information of a file.


unlink() :

By using this function, we can delete a file permanently from hard disk.

Example :

<?php unlink("index.html"); ?>


copy() :

By using this function, we can copy a file from one location to another location.

Example :

<?php copy("index.php", "C :/ind.php"); ?>


rename() :

By using this function, we can rename a file name.

Example :

<?php rename("demo.php", "demoxyz.php"); ?>