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:
當訪問一個檔案是,函數必須遵循下面這些法則:
- If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of *filename*
如果設定了FILE_USE_INCLUDE_PATH,那麼將檢查*filename*副本的內建路徑
- Create the file if it does not exist
如果檔案不存在,將建立一個檔案
- Open the file
開啟檔案
- Lock the file if LOCK_EX is set
如果設定了LOCK_EX,那將對檔案上鎖
- If FILE_APPEND is set, move to the end of the file. Otherwise, clear the file content
如果設定了FILE_APPEND,那麼將移至檔案末尾;否則的話,將會清除檔案的內容
- Write the data into the file
向檔案中寫入資料
- 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:
上述代碼將輸出下面的結果: