Web Development Lesson 14 of 17
PHP include, require and Working with Files
Reuse headers, footers and functions with include and require, read and write text files with file_get_contents and file_put_contents, work with JSON files, and handle file uploads safely.
Real websites repeat the same header, navigation and footer on every page. PHP's include and require let you write them once. PHP can also read and write files on the server, which is useful for simple data, logs and uploads.
include and require
header.php:
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"><title><?= $title ?? "My School" ?></title></head>
<body>
<nav><a href="index.php">Home</a> | <a href="about.php">About</a></nav>
footer.php:
<footer>© <?= date("Y") ?> Rizal National High School</footer>
</body>
</html>
about.php:
<?php
$title = "About Us";
require "header.php";
?>
<h1>About Us</h1>
<p>We are a public high school in Rizal province.</p>
<?php require "footer.php"; ?>
Change the navigation once in header.php, and every page updates.
| Statement | If the file is missing |
|---|---|
include | Warning; the page continues |
require | Fatal error; the page stops |
include_once / require_once | Same, but never loads the same file twice |
Use require_once for files with functions or settings (loading them twice causes "cannot redeclare" errors), and require for templates like headers.
Reading and writing text files
<?php
file_put_contents("visits.txt", "1");
$count = (int) file_get_contents("visits.txt");
$count++;
file_put_contents("visits.txt", (string) $count);
echo "Visitor number $count\n";
file_put_contents("log.txt", "Ana logged in\n", FILE_APPEND);
file_put_contents("log.txt", "Ben logged in\n", FILE_APPEND);
foreach (file("log.txt", FILE_IGNORE_NEW_LINES) as $line) {
echo "- $line\n";
}
var_dump(file_exists("missing.txt"));
Output
Visitor number 2 - Ana logged in - Ben logged in bool(false)
file_get_contents($f)reads a whole file into a string.file_put_contents($f, $text)writes (replaces) a file; addFILE_APPENDto add to the end.file($f)reads lines into an array.file_exists($f)checks before reading.
Saving data as JSON
For small amounts of structured data, store arrays as JSON:
<?php
$todos = [
["task" => "Assignment sa Math", "done" => false],
["task" => "Review for quiz", "done" => true],
];
file_put_contents("todos.json", json_encode($todos, JSON_PRETTY_PRINT));
$loaded = json_decode(file_get_contents("todos.json"), true);
foreach ($loaded as $t) {
echo ($t["done"] ? "[x] " : "[ ] ") . $t["task"] . "\n";
}
Output
[ ] Assignment sa Math [x] Review for quiz
json_decode(..., true) returns associative arrays. For anything with many users or lots of data, use a database instead (next lessons).
File uploads
<form method="post" enctype="multipart/form-data">
<input type="file" name="photo" accept="image/*">
<button>Upload</button>
</form>
<?php
if (isset($_FILES["photo"]) && $_FILES["photo"]["error"] === UPLOAD_ERR_OK) {
$tmp = $_FILES["photo"]["tmp_name"];
$type = mime_content_type($tmp);
$allowed = ["image/jpeg" => "jpg", "image/png" => "png"];
if (!isset($allowed[$type]) || $_FILES["photo"]["size"] > 2_000_000) {
echo "Only JPG or PNG up to 2 MB.";
} else {
$newName = bin2hex(random_bytes(8)) . "." . $allowed[$type];
move_uploaded_file($tmp, __DIR__ . "/uploads/" . $newName);
echo "Uploaded!";
}
}
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04