php file_put_contents 函數
file_put_contents
( PHP 5中)
file_put_contents -寫一個字串到一個檔案
描述
國際file_put_contents (字串$檔案名稱,混合$資料[摘要$國旗= 0 [ ,資源$背景] ] )
這一功能是相同的要求fopen ( )函數, fwrite ( )和fclose ( )先後將資料寫入一個檔案。
如果檔案不存在,該檔案的建立。否則,現有的檔案被覆蓋,除非FILE_APPEND旗幟設定。
參數
檔案名稱
檔案路徑在哪裡寫的資料。
資料
這些資料給我們寫信。可以是一個字串,數組或流資源(上面解釋) 。
如果資料流的資源,剩下的緩衝區的流將被複製到指定的檔案。這是類似使用stream_copy_to_stream ( ) 。
您還可以指定資料參數作為一個單一的層面陣列。這相當於file_put_contents ( $檔案名稱,爆( '' , $陣列) ) 。
旗幟
國旗的價值可任意組合下列旗幟(與一些限制) ,加入的二進位或( | )操作符。
可懸掛國旗描述
FILE_USE_INCLUDE_PATH搜尋檔案名稱中包含目錄。見include_path中擷取更多資訊。
FILE_APPEND如果檔案filename已經存在,附加資料的檔案,而不是覆蓋它。
LOCK_EX獲得獨佔鎖定的檔案,同時著手寫作。
FILE_TEXT資料寫入的文字模式。如果已啟用的Unicode語義,預設字元編碼是UTF - 8 。您可以指定一個不同的編碼,建立一個自訂的範圍內或使用stream_default_encoding ( )來更改預設的。此標誌不能用於FILE_BINARY 。此標誌只適用於自PHP 6 。
FILE_BINARY資料將被寫入二進位模式。這是預設設定,並不能用於FILE_TEXT 。此標誌只適用於自PHP 6 。
背景
資源的有效範圍內建立stream_context_create ( ) 。
傳回值
該函數返回的位元組數是寫入檔案,或FALSE的失敗。
執行個體
例如# 1使用簡單的例子
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smithn";
// Write the contents back to the file
file_put_contents($file, $current);
?>
$file = 'people.txt';
// The new person to add to the file
$person = "John Smithn";
// Write the contents to the file,
// using the FILE_APPEND flag to append the content to the end of the file
// and the LOCK_EX flag to prevent anyone else writing to the file at the same time
file_put_contents($file, $person, FILE_APPEND | LOCK_EX);
?>
http://www.bkjia.com/PHPjc/445472.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445472.htmlTechArticlephp file_put_contents 函數 file_put_contents ( PHP 5中) file_put_contents -寫一個字串到一個檔案 描述 國際file_put_contents (字串$檔案名稱,混合$資料...