PHP - File Handling
File handling in PHP involves reading from and writing to files on the server or performing various operations like creating, deleting, and modifying files. PHP provides a rich set of functions and methods to handle files efficiently. Here’s a comprehensive guide to file handling in PHP:
1. Opening and Closing Files
To open a file in PHP, you use the fopen()
function, which returns a file pointer/resource that is
used to perform operations on the file. After performing operations, you should close the file using
fclose()
to free up resources.
Opening a File
php<?php
$filename = "example.txt";
$file = fopen($filename, "r") or die("Unable to open file!");
// Perform operations on the file
fclose($file); // Close the file
?>
Modes for fopen()
"r"
: Read only. Starts at the beginning of the file."w"
: Write only. Truncates the file to zero length or creates a new file for writing."a"
: Write only. Opens and writes to the end of the file or creates a new file for writing."r+"
: Read/Write. Starts at the beginning of the file."w+"
: Read/Write. Truncates the file to zero length or creates a new file for reading and writing."a+"
: Read/Write. Opens and writes to the end of the file or creates a new file for reading and writing.
2. Reading from Files
PHP provides several functions to read data from files based on different needs.
fgets()
Reads a line from an open file.
php<?php
$filename = "example.txt";
$file = fopen($filename, "r") or die("Unable to open file!");
// Read lines until end of file
while (!feof($file)) {
echo fgets($file) . "<br>";
}
fclose($file);
?>
fread()
Reads a specified number of bytes from a file.
php<?php
$filename = "example.txt";
$file = fopen($filename, "r") or die("Unable to open file!");
// Read 20 bytes from the file
echo fread($file, 20);
fclose($file);
?>
3. Writing to Files
PHP allows you to write data to files using functions like fwrite()
.
fwrite()
Writes to an open file.
php<?php
$filename = "newfile.txt";
$file = fopen($filename, "w") or die("Unable to open file!");
$text = "Hello, World!";
fwrite($file, $text);
fclose($file);
?>
4. Appending to Files
To append data to the end of a file without truncating its contents, you can use "a"
mode with
fwrite()
.
php<?php
$filename = "existingfile.txt";
$file = fopen($filename, "a") or die("Unable to open file!");
$text = "New data appended!";
fwrite($file, $text);
fclose($file);
?>
5. Checking File Existence
You can check if a file exists before attempting to open or modify it using file_exists()
.
php<?php
$filename = "example.txt";
if (file_exists($filename)) {
echo "The file $filename exists.";
} else {
echo "The file $filename does not exist.";
}
?>
6. Deleting Files
PHP provides unlink()
to delete files from the server.
php<?php
$filename = "filetobedeleted.txt";
if (file_exists($filename)) {
unlink($filename);
echo "File $filename deleted successfully.";
} else {
echo "File $filename does not exist.";
}
?>
7. File Handling Functions
PHP offers various functions to manipulate and interact with files, such as:
file_get_contents()
: Reads entire file into a string.file_put_contents()
: Writes a string to a file.rename()
: Renames a file or directory.filesize()
: Gets the file size.filemtime()
,filectime()
,fileatime()
: Gets file modification, change, and access times respectively.
Example: Using file_get_contents()
and file_put_contents()
php<?php
// Read file content
$content = file_get_contents("example.txt");
echo $content;
// Write content to a new file
$newContent = "This is new content.";
file_put_contents("newfile.txt", $newContent);
?>