php file_put_contents() 函數

來源:互聯網
上載者:User
Definition and Usage
定義和用法

The file_put_contents() writes a string to a file.
file_put_contents()函數的作用是:將一個字串寫入檔案。

This function follows these rules when accessing a file:
當訪問一個檔案是,函數必須遵循下面這些法則:

  1. If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of *filename*
    如果設定了FILE_USE_INCLUDE_PATH,那麼將檢查*filename*副本的內建路徑
  2. Create the file if it does not exist
    如果檔案不存在,將建立一個檔案
  3. Open the file
    開啟檔案
  4. Lock the file if LOCK_EX is set
    如果設定了LOCK_EX,那將對檔案上鎖
  5. If FILE_APPEND is set, move to the end of the file. Otherwise, clear the file content
    如果設定了FILE_APPEND,那麼將移至檔案末尾;否則的話,將會清除檔案的內容
  6. Write the data into the file
    向檔案中寫入資料
  7. Close the file and release any locks
    關閉檔案並對所有檔案解鎖

This function returns the number of character written into the file on success, or FALSE on failure.
如果函數運行成功,將返回寫入檔案中的字元數量;如果失敗,則返回False。

Syntax
文法
file_put_contents(file,data,mode,context)
Parameter
參數
Description
描述
file Required. Specifies the file to write to. If the file does not exist, this function will create one
必要參數。指定寫入檔案對象。如果該檔案不存在,函數將自動建立該檔案
data Required. The data to write to the file. Can be a string, an array or a data stream
必要參數。指定寫入檔案的資料,可以使一個字串或者是一個數組
mode Optional. Specifies how to open/write to the file. Possible values:
選擇性參數。指定開啟/書寫檔案的方法:
  • FILE_USE_INCLUDE_PATH
  • FILE_APPEND
  • LOCK_EX
context Optional. Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream.
選擇性參數。指定需要進行處理的檔案context[內容]。Context是一組選項,你可以通過它修改你的文字屬性
Tips and Notes
注意點

Note: Use FILE_APPEND to avoid deleting the existing content of the file.
注意:使用FILE_APPEND避免刪除檔案的現存內容。

Example
案例
<?phpecho file_put_contents("test.txt","Hello World. Testing!");?> 

The output of the code above will be:
上述代碼將輸出下面的結果:

21

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.