在PHP中,fwrite()
函數用于將數據寫入文件。若要向文件追加內容,請將FILE_APPEND
常量作為第三個參數傳遞給fwrite()
函數。以下是一個示例:
<?php
$file = 'example.txt';
$content = 'This is a new line.\n';
// 追加內容到文件
$result = fwrite($file, $content, FILE_APPEND);
if ($result === false) {
echo 'Error writing to file.';
} else {
echo 'Content appended successfully.';
}
?>
在這個示例中,我們首先指定要寫入的文件名(example.txt
),然后定義要追加的內容($content
)。接下來,我們使用fwrite()
函數將內容追加到文件中,并將FILE_APPEND
常量作為第三個參數傳遞。最后,我們檢查fwrite()
函數的返回值以確定操作是否成功。