ファイルの作成:
<?php
$file = 'example.txt';
$content = 'Hello, World!';
// ファイルを開く(存在しない場合は新規作成)
$handle = fopen($file, 'w');
if ($handle) {
// ファイルに書き込む
fwrite($handle, $content);
// ファイルを閉じる
fclose($handle);
echo "File '$file' has been created.";
} else {
echo "Unable to create the file '$file'.";
}
?>
ファイルの編集:
<?php
$file = 'example.txt';
$content = ' This is additional content.';
// ファイルを開く(追記モード)
$handle = fopen($file, 'a');
if ($handle) {
// ファイルに書き込む
fwrite($handle, $content);
// ファイルを閉じる
fclose($handle);
echo "File '$file' has been updated.";
} else {
echo "Unable to update the file '$file'.";
}
?>
ファイルの削除:
<?php
$file = 'example.txt';
// ファイルを削除
if (unlink($file)) {
echo "File '$file' has been deleted.";
} else {
echo "Unable to delete the file '$file'.";
}
?>