In this PHP tutorial, you will learn all about the file handlings in PHP. We will discuss in detail about different file handling functions such as fopen(), fclose(), fwrite(), fread().
The file handling in PHP is used to create a file, read a file, write into a file, append into files and also close and delete a file in the system.
The function used in File handling are:
The PHP fopen() function opens a file or URL and returns a resource. ‘filename’ and ‘mode’ are the two arguments to the fopen() method. The ‘filename’ variable represents the file to be opened, and the ‘mode’ variable represents the file mode, such as read-only, read-write, or write-only.
Syntax
fopen(filename, mode);
Example
$file = fopen("c:\\folder\\file.txt", "r");
Modes |
Description |
---|---|
r |
It is to specify that the file is open for read the file. The pointer starts at the beginning of the file. |
r+ |
It is to specify that the file opens for read and write the file. The pointer starts at the beginning of the file. |
w |
It is to specify that the file is open for write the file. The pointer starts at the beginning of the file. If the file is not found it will create a file. |
w+ |
It is to specify that the file opens for write and read the file. The pointer starts at the beginning of the file. If the file is not found it will create a file. |
a |
It is to specify that the file is open for write the file. The pointer starts at the end of the file. If the file is not found it will create a file. |
a+ |
It is to specify that the file opens for write and read the file. The pointer starts at the end of the file. If the file is not found it will create a file. |
x |
It is to specify to create a file for write. If the file is already then it will return FALSE |
x+ |
It is to specify to create a file for write and read. If the file is already then it will return FALSE |
The fread() function is used to read the data from the specified file. PHP has various functions for reading data from files. There are methods for reading full file data, reading data line by line, and reading data character by character.
The functions used to read the data from the file are:
To read a file using fread() we have to pass two arguments: the first is the filename and the second is the length of the data.
Syntax
fread (file , length ) ;
Popular Programming languages
1) Python
2) JavaScript
3) Java
4) PHP
5) TypeScript
6) Go
7) Flutter
8) C++
9) C#
10) SQL
$file = fopen("c:\\folder\\file.txt", "r");
$file_data = fread($file, filesize("c:\\folder\\file.txt "));
echo "$file_data";
fclose($file);
Output:
Popular Programming languages 1) Python 2) JavaScript 3) Java 4) PHP 5) TypeScript 6) Go 7) Flutter 8) C++ 9) C# 10) SQL
In the above example, we can see that we have opened an external file “file.txt” which contains the data of the popular programming languages and we can see that using the fread() function we are able to read that data in that file.
To read a file using fgets() is used to read a single line from the specified file.
Syntax
fgets(file);
Popular Programming languages
1) Python
2) JavaScript
3) Java
4) PHP
5) TypeScript
6) Go
7) Flutter
8) C++
9) C#
10) SQL
$file = fopen("c:\\folder\\file.txt", "r");
$file_data = fgets($file);
echo "$file_data";
fclose($file);
Output:
Popular Programming languages
In the above example, we can see that we have used the fgets() function to read the file and it has only read the first line from the specified file.
To read a file using fgets() is used to read a single character from the specified file. To fetch all data using the fgetc() function, use !feof() function inside the while loop.
Syntax
fgets(file);
Popular Programming languages
1) Python
2) JavaScript
3) Java
4) PHP
5) TypeScript
6) Go
7) Flutter
8) C++
9) C#
10) SQL
$file = fopen("c:\\folder\\file.txt", "r");
$file_data = fgetc($file);
echo "$file_data";
fclose($file);
Output:
P
In the above example we can see that we have used the fgetc() function to read the file and it has only read the first character from the specified file.
$file = fopen("c:\\folder\\file.txt", "r");
while (!feof($file)) {
echo fgetc($file);
}
fclose($file);
Popular Programming languages
1) Python
2) JavaScript
3) Java
4) PHP
5) TypeScript
6) Go
7) Flutter
8) C++
9) C#
10) SQL
Output:
Popular Programming languages 1) Python 2) JavaScript 3) Java 4) PHP 5) TypeScript 6) Go 7) Flutter 8) C++ 9) C# 10) SQL
To write data to a file, the PHP fwrite() and fputs() methods are used. To write data into a file, we have to specify any of the w, r+, w+, x, x+ modes.
The PHP fwrite() method is used to write the string's content to a file.
If we open the file with mode w, w+, r+, x, x+ and when we call the fwrite() method, the prior data in the file will be erased and the new data is written. And if the file doesn’t exist then it will create a new file and write into it.
Syntax
fwrite(file, string);
Example
$file = fopen("file.txt", "w");
fwrite($file, "Welcome to learnetutorials.com");
fclose($file);
This program will create new file with the following content
Output:
Welcome to learnetutorials.com
Using the a or a+ mode in the fopen() method, we could append data to a file. The PHP fwrite() method is used to append the string's content to the specified file. By this we can add a new string to the file instead of overwriting it.
Syntax
fwrite(file, string););
Example
$file = fopen("file.txt", "a");
fwrite($file, "Where you learn programming");
fclose($file);
“file.txt” before the execution of the code
Welcome to learnetutorials.com!
“file.txt” after the execution of the code
Welcome to learnetutorials.com! Where you learn programming
We use the fclose() function to close a file that is opened in the script. It's an appropriate programming habit to close all files after you're done with them.
Syntax
fclose(file);
Example
$file = fopen("file.txt", "a");
fclose($file);
In PHP to delete a file, we use the unlink() function. The unlink() method only takes one argument which is the file name. If the file is not deleted, PHP unlink() will return an E_WARNING error. If the file is successfully deleted then it will return TRUE; otherwise, it returns FALSE.
Syntax
unlink(file);
Example
$deletion = unlink('file.txt');
if ($deletion) {
echo "The file have been successfully deleted";
} else {
echo "The file has not been deleted";
}
Output:
It will return the message and the file will be deleted