PHP File Handling
File handling in PHP is used to create, open, read, write, delete, and manipulate files on a server. It is used when you need to store data persistently or handle files uploaded by users. PHP provides several built-in functions to make file handling easy and secure.
Table of Content
Common File Handling Functions in PHP
- fopen() – Opens a file
- fclose() – Closes a file
- fread() – Reads data from a file
- fwrite() – Writes data to a file
- file_exists() – Checks if a file exists
- unlink() – Deletes a file
What is File Handling in PHP?
File handling is the process of interacting with files on the server, such as reading files, writing to a file, creating new files, or deleting existing ones. File handling is essential for applications that require the storage and retrieval of data, such as logging systems, user-generated content, or file uploads.
Opening and Closing Files
Before you can read or write to a file, you need to open it using the fopen() function, which returns a file pointer resource. Once you’re done working with the file, you should close it using fclose() to free up resources.
<?php
// Open the file in read mode
$file = fopen("gfg.txt", "r");
if ($file) {
echo "File opened successfully!";
fclose($file); // Close the file
} else {
echo "Failed to open the file.";
}
?>
File Modes in PHP
Files can be opened in any of the following modes:
- “w” – Opens a file for writing only. If file does not exist then new file is created and if file already exists then file will be truncated (contents of file is erased).
- “r” – File is open for reading only.
- “a” – File is open for writing only. File pointer points to end of file. Existing data in file is preserved.
- “w+” – Opens file for reading and writing both. If file not exist then new file is created and if file already exists then contents of file is erased.
- “r+” – File is open for reading and writing both.
- “a+” – File is open for write/read. File pointer points to end of file. Existing data in file is preserved. If file is not there then new file is created.
- “x” – New file is created for write only.
Reading from Files
There are two ways to read the contents of a file in PHP. These are –
1. Reading the Entire File
You can read the entire content of a file using the fread() function or the file_get_contents() function.
<?php
$file = fopen("gfg.txt", "r");
$content = fread($file, filesize("gfg.txt"));
echo $content;
fclose($file);
?>
2. Reading a File Line by Line
You can use the fgets() function to read a file line by line.
<?php
$file = fopen("gfg.txt", "r");
if ($file) {
while (($line = fgets($file)) !== false) {
echo $line . "<br>";
}
fclose($file);
}
?>
Writing to Files
You can write to files using the fwrite() function. It writes data to an open file in the specified mode.
<?php
// Open the file in write mode
$file = fopen("gfg.txt", 'w');
if ($file) {
$text = "Hello world\n";
fwrite($file, $text);
fclose($file);
}
?>
Deleting Files
Use the unlink() function to delete the file in PHP.
<?php
if (file_exists("gfg.txt")) {
unlink("gfg.txt");
echo "File deleted successfully!";
} else {
echo "File does not exist.";
}
?>