For any application, we need to have file handling. Some tasks require files for the program to work. The file handling in PHP is similar to the file handling in any other programming language. There are many functions to perform the file handling operations in PHP. Those functions are listed below:
fopen()
fread()
fwrite()
fclose()
fopen()
functionTo use the file, first of all, we have to open the file by using the open()
function. The fopen()
function accepts two arguments one is the name of the file and the second one is the mode in which we want to open the file.
The different modes to open the file are listed below:
r:
It only reads the existing file.
w:
It is used to write a file. If the file doesn’t exist, then it creates the new file and then opens it for writing the file. If the file already exists, then, first, it erases all the contents of the file and then opens it.
a:
The file is opened for write-only. But the file pointer points to the end of the file, so the existing data is preserved.
r+:
It is used to read and write the existing file.
w+:
It is used to read and write a file. If the file doesn’t exist, then it creates the new file and then opens it for writing the file. If the file already exists, then, first, it erases all the contents of the file and then opens it.
a+:
The file is opened for read and write. But the file pointer points to the end of the file, so the existing data is preserved.
x:
To create a file with write-only access.
Let’s look at the example below:
<?php$file = fopen("tst.txt",'x');?>
fopen()
function and pass it the file name and the mode in which we want the file to behave.fread()
functionAfter the file is opened, we use the fread()
function to read the file. This function takes two arguments:
Let’s look at the code below:
<?php$name = "demo.txt";$file = fopen( $name, 'r' );$size = filesize( $name );$filedata = fread( $file, $size );echo $filedata?>
Line 2: We store the file’s name in a variable.
Line 3: We open the file in read-only mode.
Line 4: We calculate the file size and store it in a variable.
Line 5: We call the fread()
function and pass the arguments.
Line 6: We print the data that we got from the file.
fwrite()
functionThe fwrite()
function is used to create a file or to append the text to the existing file. This function also takes two arguments:
Let’s look at the code below:
<?php$file = fopen("test.txt", 'w');$text = "I am Behzad\n";fwrite($file, $text);$file = fopen( "test.txt", 'r' );$size = filesize( "test.txt" );$filedata = fread( $file, $size );echo $filedata?>
Line 2: We open the file.
Line 3: We store the text we want to write in the file.
Line 4: We call the fwrite()
function and pass the arguments to the function.
Line 5: We open the file once again but this time in read-only mode.
Line 6: We calculate the file size and store it in a variable.
Line 7: We call the fread()
function and pass the arguments to the function.
Line 8: We print the data that we got from the file to see if the file is updated.
fclose()
fucnctionWhen we use the file then we have to close the file by using the fclose()
function.
Let’s look at the code below:
<?php$file = fopen("demo.txt", 'r');fclose($file);?>
Line 2: We open the file by using the fopen()
function.
Line 3: We close the file by using the fclose()
function.