PHP File Handling Introduction
In this page:
$handle = fopen("filename.txt", "mode"); // false on failure
fwrite($handle, $data);
fclose($handle);
Opening Files with fopen()
PHP can read from and write to files on the server's filesystem, letting a script persist data, log activity, or generate downloadable content without needing a database for every task.
Example: Opening Files with fopen()
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Check whether `$handle`
if ($handle) {
// Print "File opened successfully" to the output
echo "File opened successfully";
// Call `fclose($handle)`
fclose($handle);
}
?>
Login to try C/C++/Java/PHP code in the editor
Writing Files with fwrite()
File operations in PHP revolve around a small set of core functions — fopen(), fread()/fwrite(), and fclose() — that mirror the open-use-close pattern found in most programming languages' file APIs.
Example: Writing Files with fwrite()
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Call `fwrite($handle, "Hello, file!")`
fwrite($handle, "Hello, file!");
// Call `fclose($handle)`
fclose($handle);
// Print `file_get_contents("data.txt")` to the output
echo file_get_contents("data.txt");
?>
Login to try C/C++/Java/PHP code in the editor
Closing Files with fclose()
Always check whether a file operation succeeded, since fopen() returns false (not an exception) on failure by default, and code that assumes success will produce confusing errors further down the line.
Example: Closing Files with fclose()
<?php
// Declare `$handle`, set to `fopen("data.txt", "w")`
$handle = fopen("data.txt", "w");
// Check whether `$handle === false`
if ($handle === false) {
// Print "Failed to open file" to the output
echo "Failed to open file";
// Otherwise, run this branch
} else {
// Call `fwrite($handle, "Saved data")`
fwrite($handle, "Saved data");
// Call `fclose($handle)`
fclose($handle);
// Print "File written and closed" to the output
echo "File written and closed";
}
?>
Login to try C/C++/Java/PHP code in the editor
Checking File Existence
File permissions and paths matter a lot in real deployments: PHP's process needs OS-level permission to read or write a given path, and a misconfigured permission is one of the most common file-handling bugs in production.
Example: Checking File Existence
<?php
// Call `file_put_contents("data.txt", "sample")`
file_put_contents("data.txt", "sample");
// Check whether `file_exists("data.txt") && is_writable("data.txt")`
if (file_exists("data.txt") && is_writable("data.txt")) {
// Print "File exists and is writable" to the output
echo "File exists and is writable";
}
?>
Login to try C/C++/Java/PHP code in the editor
Deleting Files with unlink()
Beyond the low-level functions, PHP offers convenience wrappers like file_get_contents() and file_put_contents() that handle the open/read-or-write/close sequence in a single call for simple cases.
Example: Deleting Files with unlink()
<?php
// Call `file_put_contents("temp.txt", "temporary data")`
file_put_contents("temp.txt", "temporary data");
// Print `file_get_contents("temp.txt") . "\n"` to the output
echo file_get_contents("temp.txt") . "\n";
// Call `unlink("temp.txt")`
unlink("temp.txt");
// Print `file_exists("temp.txt") ? "Still exists" : "Deleted successfully"` to the output
echo file_exists("temp.txt") ? "Still exists" : "Deleted successfully";
?>
Login to try C/C++/Java/PHP code in the editor
- Not checking whether
fopenreturned false before using the handle. - Forgetting
fclose, so buffered data may not be written. - Using the wrong mode, such as
w, which erases an existing file.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: