File Handling in php
php provides built-in functions to create, read, write, and delete files on the server.
Opening a Fileโ
fopen() opens a file and returns a file handle:
<?php
$file = fopen("data.txt", "r"); // open for reading
?>
File Modesโ
| Mode | Description |
|---|---|
r | Read only (file must exist) |
w | Write only (creates or truncates file) |
a | Append (creates if not exists) |
r+ | Read and write |
w+ | Read and write (truncates) |
a+ | Read and append |
x | Write only (fails if file exists) |
Reading a Fileโ
Read Entire Fileโ
<?php
// Read all contents at once
$content = file_get_contents("data.txt");
echo $content;
?>
Read Line by Lineโ
<?php
$file = fopen("data.txt", "r");
if ($file) {
while (($line = fgets($file)) !== false) {
echo htmlspecialchars($line) . "<br>";
}
fclose($file);
}
?>
Read Into Arrayโ
<?php
$lines = file("data.txt", FILE_IGNORE_NEW_LINES);
foreach ($lines as $line) {
echo $line . "<br>";
}
?>
Writing to a Fileโ
Overwrite (Write Mode)โ
<?php
file_put_contents("output.txt", "Hello, php!\n");
// Creates or overwrites the file
?>
Append to a Fileโ
<?php
file_put_contents("log.txt", "New entry\n", FILE_APPEND);
?>
Using fwrite()โ
<?php
$file = fopen("notes.txt", "w");
fwrite($file, "First line\n");
fwrite($file, "Second line\n");
fclose($file);
?>
Checking File Existenceโ
<?php
if (file_exists("data.txt")) {
echo "File exists!";
} else {
echo "File not found.";
}
?>
File Informationโ
<?php
echo filesize("data.txt"); // size in bytes
echo filemtime("data.txt"); // last modified timestamp
echo filetype("data.txt"); // file, dir, link, etc.
echo pathinfo("data.txt", PATHINFO_EXTENSION); // txt
echo basename("/path/to/data.txt"); // data.txt
echo dirname("/path/to/data.txt"); // /path/to
?>
Deleting a Fileโ
<?php
if (file_exists("temp.txt")) {
unlink("temp.txt");
echo "File deleted.";
}
?>
Copying and Renamingโ
<?php
copy("source.txt", "backup.txt"); // copy
rename("old_name.txt", "new_name.txt"); // rename/move
?>
Working with Directoriesโ
<?php
// Create directory
mkdir("uploads", 0755, true); // recursive
// List directory contents
$files = scandir("uploads");
foreach ($files as $file) {
if ($file !== "." && $file !== "..") {
echo $file . "<br>";
}
}
// Remove directory (must be empty)
rmdir("empty_folder");
?>
Reading CSV Filesโ
<?php
$file = fopen("data.csv", "r");
while (($row = fgetcsv($file)) !== false) {
print_r($row); // array of values per row
}
fclose($file);
?>
Writing CSV Filesโ
<?php
$file = fopen("output.csv", "w");
fputcsv($file, ["Name", "Age", "City"]);
fputcsv($file, ["Alice", 30, "Delhi"]);
fputcsv($file, ["Bob", 25, "Mumbai"]);
fclose($file);
?>
Security Note: Always validate file paths and extensions when handling user-submitted file names to prevent directory traversal attacks.
Finished reading? Mark this topic as complete.