PHP File Handling Functions: Reading, Writing, Opening and Closing File
Processing files concept is one of the important concepts in PHP.it is used to interact with a file on server.interacting means reading a file, writing a file,closing a file,opening a file .these are some actions performed on PHP for interaction.we are using some file functions for communication.
Types of file operations in PHP
1.reading a file
2.writing a file
3.opening a file
4.closing a file
5.uploading a file
1.Reading a file
Explanation:
You can use file reading to read the file content without any access to change it.
We can read that file entirely at once or line by line as possible.
2.Writing a file
Writing file is used to write the content in existing file or new file.
3.opening a file
Once file is saved .we can open that file based name of file at n number types.
4.closing a file
Closing a file is used for close that file after completion of work.
5.uploading a file
Uploading a file is used to upload or store the file on server.
PHP File Handling Functions
fwrite()-writing a file
fread()-reading a file
fopen()-opening a file
fclose()-closing a file
file_exists()-checking whether file exist or not.
unlink()-it is used to delete the file.
PHP File Modes
- r
- w
- a
- r+
- w+
- a+
- x
Modes Files
- r- only to read the file
- w-opening and write the file
- a- a for append it only write the existing file at end of the file.
- r+ -it is used for both read/write a file
- w+ – it is used for both Read/write and modify
- a+ -appending the content at existing file for both read/write.
- x- creating a new file in PHP.
Writing and Reading a File in PHP
In PHP file processing consists reading , writing and opening,closing a file.before reading a file we should open a file .after completion of reading file should be closed using fclose() function.
Syntax is : fopen(“file.txt”,mode of file)
Program:
- <?PHP
$f=fopen(“hello.txt”, “r”);
//here fopen is used to open a file that file name is hello.txt.after that the mode of the file is reading mode so that we write r . - $f=fopen(“hello.txt”, “w”);
//here fopen is used to open a file that file name is hello.txt is a text file .we are using writing mode to write a program on a file. - $f=fopen(“hello.txt” , “a”);
fopen is used to open the file.that file name is hello.txt file.here we are using a mode.a for append.it adds the content for the existing file.
Reading a File Example
<?PHP
$f=fopen(“hello.txt”, “r”);
echo fread($f);
fclose($f);
?>
First file is opening with the file name of hello.there are using reading mode for read that file.after completion of reading close the file.
Writing a File Example
<?PHP
$f=fopen(“hello.txt”, “w”);
echo fwrite($f);
fclose($f);
?>
File is opened first.after that there write mode is activated to write a program on file .after completion of writing,closing a file.
Other PHP File Functions
There are five file functions in PHP.it is used to check condition by using file functions.
1.file_exist()
2.filesize()
3.unlink()
4.rename()
5.copy()
1.file_exist():
In php file_exist() function is used to check whether given file is exist or not
Program:
<?php
if($file_exists(“hello.txt”))
{
echo “file exist”;
}
?>
Output:
file exist
2.filesize()
filesize() in php is used to check the size of given file.
Program:
<?php
echo filesize(“hello.txt”);
?>
3.unlink()
unlink() function is used to delete the file
Program:
<?php
echo unlink(“hello.txt”);
?>
4.rename()
In php rename() is used to change the existing file name to new filename.
<?php
rename(“hello.txt” , “hi.txt”);
?>
5.copy()
In php copy() function is used to copy the file and store the duplicate file.
<?php
copy(“hello.txt”,”hi.txt”);
?>
File LOCKING :
File LOCKING () is the main function in php.it is used to lock the file from modifying the unknown person.this locking is maintains privacy for the file.
There are two functions file locking()
1.LOCK_EX
2.LOCK_UN
Lock_ex():
Lock_ex is used for exclusive lock on file.
Lock_Un():
Lock_Un() is used to unlock a file.
<?php
$f=fopen(“hello.txt”, “r”);
flock(“$f”,LOCK_EX);
fwrite(“$f”,”good morning”);
flock(“$f”,LOCK_UN);
fclose($f);
?>
Frequently answeres Questions with answers
1.What is file handling in PHP?
A.file handling functions are used to read,write,create,append the content of the file.
2.How to open a file in PHP using fopen()?
A.fopen(“filename.txt”,”mode”);
3.How to read a file in PHP with example?
A .echo fopen(“filename.txt”,”r”);
4.How to write data into a file using PHP?
A.<?php
$fil=fopen(“hello.txt”,”w”);
fwrite(“$fil”,”good evening”);
fclose($fil);
?>
5.What are file modes in PHP fopen()?
A.r -readmode
w-write mode
a-append mode
r+- both read and write mode
w+-both write and read mode
x- create a file
6.Difference between fread() and file_get_contents() in PHP?
fread()-fread is written under fopen() function.it used specific bytes.suitable for large files.
file_get_contents()_it does not require a fopen() function.it is used for small files.it reads entire file .
7.How to append data to a file in PHP?
A.fopen(“filename.txt”,”a”);
8.How to read file line by line in PHP?
A.fgets() function is used to read file line by line.
Syntax: fgets($filename);
9.How to create and delete files in PHP?
A .<?php
$fil=fopen(“hello.txt”,”r”);
fclose(“$fil);
echo “successful created”;
?>
$fil=fopen(“hello.txt”,”r”);
Unlink($fil);
fclose(“$fil);
echo “successful deleted”;
?>
10.Explain PHP file handling functions with examples.
A.fopen()-opening a file
fclose()-closing a file
fread()-used to read a file content
fgets()-used to read a file line by line.
Unlink()-it is used to delete a file
rename()-it is used to change the existing file name.
copy()-copy is used to copy the file content.
2 thoughts on “PHP File Handling Functions: Read, Write, Open & Close Files with Examples”