php file_put_contents 函數_PHP教程

來源:互聯網
上載者:User
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 (字串$檔案名稱,混合$資料...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.