
9.6
エラーチェック
191
例
9-15
読み込みパーミッションの検査
$template_file = 'page-template.html';
if (is_readable($template_file)) {
$template = file_get_contents($template_file);
} else {
print "Can't read template file.";
}
例9-16 は、ファイルが書き込み可能であることを確認してから
fopen()
と
fwrite()
でファイル
に行を追加します。
例
9-16
書き込みパーミッションの検査
$log_file = '/var/log/users.log';
if (is_writeable($log_file)) {
$fh = fopen($log_file,'ab');
fwrite($fh, $_SESSION['username'
] . ' at ' . strftime('%c') . "\n");
fclose($fh);
} else {
print "Cant write to log file.";
}
9.6
エラーチェック
本章ではこれまで、エラーチェックをせずに例を示してきました。そのために例を短くできた
ので、
file_get_contents()
、
fopen()
、
fgetcsv()
などのファイル操作関数に専念できました。デー
タベースとのやり取りと同じように、ファイルの操作はプログラム外部のリソースとのやり取りを ...