读写文件、记录事件日志、安全地接收上传,并用 JSON 存储你的数据。
在 Kodokon 中打开本课读取一个文件只需一个函数:file_get_contents() 把全部内容作为一个字符串返回,file() 则把它拆分成一个按行组成的数组。在读取之前,用 is_readable() 检查访问权限:一个错误的路径会返回 false 并伴随一个警告。
<?php
declare(strict_types=1);
$path = __DIR__ . '/notes.txt';
if (!is_readable($path)) {
exit("File not found or unreadable.");
}
$content = (string) file_get_contents($path);
echo nl2br(htmlspecialchars($content, ENT_QUOTES));
$lines = file(
$path,
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
);
echo count($lines) . " non-empty line(s)";写入时,file_put_contents() 默认会替换全部内容。两个标志会改变这一行为:FILE_APPEND 会追加到末尾而不是覆盖,LOCK_EX 会设置一把排他锁,这样两个同时到来的请求就不会把各自的写入混在一起。这正是一个极简应用日志的实现秘诀。
<?php
declare(strict_types=1);
$logFile = __DIR__ . '/app.log';
$line = sprintf(
"[%s] New order from %s\n",
date('Y-m-d H:i:s'),
'alice@example.com'
);
file_put_contents(
$logFile,
$line,
FILE_APPEND | LOCK_EX
);
echo "Event logged.";文件上传值得格外小心。表单必须声明 enctype="multipart/form-data",随后 PHP 会在 $_FILES 中描述接收到的文件。要验证一切:错误码、大小,以及由 finfo 检测出的真实类型 - 绝不要相信扩展名或浏览器所声称的类型。最后,只有 move_uploaded_file() 才应该用来移动那个临时文件。
<?php
declare(strict_types=1);
$maxSize = 2 * 1024 * 1024;
$file = $_FILES['avatar'] ?? null;
if ($file === null || $file['error'] !== UPLOAD_ERR_OK) {
exit("No valid file received.");
}
if ($file['size'] > $maxSize) {
exit("File too large (2 MB max).");
}
$info = new finfo(FILEINFO_MIME_TYPE);
$mime = $info->file($file['tmp_name']);
$allowed = ['image/jpeg' => 'jpg', 'image/png' => 'png'];
if (!isset($allowed[$mime])) {
exit("Format not allowed.");
}
$name = bin2hex(random_bytes(8));
$target = __DIR__ . '/uploads/' . $name
. '.' . $allowed[$mime];
move_uploaded_file($file['tmp_name'], $target);
echo "Image saved.";JSON 格式是 PHP 与外部世界之间天然的桥梁:json_encode() 会把一个数组序列化,json_decode() 则做相反的事。把 true 作为第二个参数传入可以得到关联数组,而 JSON_THROW_ON_ERROR 标志能把格式错误的 JSON 变成一个清晰的异常,而不是一个悄无声息的 null。
<?php
declare(strict_types=1);
$path = __DIR__ . '/tasks.json';
$json = is_readable($path)
? (string) file_get_contents($path)
: '[]';
$tasks = json_decode(
$json,
true,
512,
JSON_THROW_ON_ERROR
);
$tasks[] = [
'title' => 'Review the lesson',
'done' => false,
];
file_put_contents(
$path,
json_encode(
$tasks,
JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
),
LOCK_EX
);
echo count($tasks) . " task(s) saved";