मुख्य कंटेंट तक स्किप करें

File Handling in php

Trushi Jasani
EditReport

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

ModeDescription
rRead only (file must exist)
wWrite only (creates or truncates file)
aAppend (creates if not exists)
r+Read and write
w+Read and write (truncates)
a+Read and append
xWrite 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.