SplFileObject::fflush()function 是 PHP 標準 PHP 庫 (SPL) 中的內置函數,用於刷新文件的輸出緩衝區。
用法:
public SplFileObject::fflush():bool
範圍:該函數不接受任何參數。
返回值: SplFileObject::fflush()如果函數成功刷新文件的輸出緩衝區,則函數返回“true”,否則,該函數將返回“false”。
程序1:下麵的程序演示了SplFileObject::flush()函數。 “output.txt” 應位於同一文件夾中。
PHP
<?php
$file = new SplFileObject("output.txt", "w");
$data = "This is some data that will be written to the file.\n";
$file->fwrite($data);
// Flush the data to the file immediately
if ($file->fflush()) {
echo "Data flushed successfully to the file.";
} else {
echo "Failed to flush data to the file.";
}
?>
輸出:
Data flushed successfully to the file.
程序2:下麵的程序演示了SplFileObject::flush()函數。 “output.txt” 應位於同一文件夾中。
PHP
<?php
$file = new SplFileObject("output.txt", "w");
$dataLines = [
"Line 1: This is the first line of data.\n",
"Line 2: This is the second line of data.\n",
"Line 3: This is the third line of data.\n",
];
foreach ($dataLines as $line) {
// Write the line to the file
$file->fwrite($line);
// Flush the data to the file
// immediately after writing each line
if ($file->fflush()) {
echo "Data flushed successfully for line: " . $line;
} else {
echo "Failed to flush data for line: " . $line;
}
}
?>
輸出:
Data flushed successfully for line: Line 1: This is the first line of data.
Data flushed successfully for line: Line 2: This is the second line of data.
Data flushed successfully for line: Line 3: This is the third line of data.
參考:https://www.php.net/manual/en/splfileobject.fflush.php
相關用法
- PHP SplFileObject ftruncate()用法及代碼示例
- PHP SplFileObject fwrite()用法及代碼示例
- PHP SplFileObject flock()用法及代碼示例
- PHP SplFileObject fputcsv()用法及代碼示例
- PHP SplFileObject fread()用法及代碼示例
- PHP SplFileObject fstat()用法及代碼示例
- PHP SplFileObject ftell()用法及代碼示例
- PHP SplFileObject fgets()用法及代碼示例
- PHP SplFileObject fgetss()用法及代碼示例
- PHP SplFileObject fgetc()用法及代碼示例
- PHP SplFileObject fpassthru()用法及代碼示例
- PHP SplFileObject getMaxLineLen()用法及代碼示例
- PHP SplFileObject rewind()用法及代碼示例
- PHP SplFileObject seek()用法及代碼示例
- PHP SplFileObject setMaxLineLen()用法及代碼示例
- PHP SplFileObject current( )用法及代碼示例
- PHP SplFileObject eof()用法及代碼示例
- PHP SplFileObject getCsvControl()用法及代碼示例
- PHP SplFileObject key()用法及代碼示例
- PHP SplFileObject next()用法及代碼示例
- PHP SplFileInfo getATime()用法及代碼示例
- PHP SplFileInfo getBasename()用法及代碼示例
- PHP SplFileInfo getCTime()用法及代碼示例
- PHP SplFileInfo getExtension()用法及代碼示例
- PHP SplFileInfo getFileInfo()用法及代碼示例
注:本文由純淨天空篩選整理自neeraj3304大神的英文原創作品 PHP SplFileObject fflush() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。