PHP中的fputs()函數是一個內置函數,用於寫入打開的文件。
fputs()函數在文件末尾或達到指定長度(作為參數傳遞)時停止,以先到者為準。
文件,字符串和必須寫入的長度作為參數發送到fputs()函數,它返回成功時寫入的字節數,失敗時返回FALSE。
fputs()函數是fwrite()函數的別名。
用法:
fputs(file, string, length)
使用的參數:
PHP中的fputs()函數接受三個參數。
- file:這是指定文件的必需參數。
- string:這是必填參數,用於指定要寫入的字符串。
- length:這是一個可選參數,它指定要寫入的最大字節數。
返回值:
它返回成功寫入的字節數,失敗返回False。
異常
- 由於fputs()是二進製安全的,因此可以使用該函數寫入二進製數據(例如圖像和字符數據)。
- 不帶length參數的fputs()函數將所有數據寫到最後,但不包括遇到的第一個第0個字節。
例子:
Input : $myfile = fopen("gfg.txt", "w"); echo fputs($myfile, "Geeksforgeeks is a portal of geeks!"); fclose($myfile); Output : 35 Input : $myfile = fopen("gfg.txt", "w"); echo fputs($myfile, "Geeksforgeeks is a portal of geeks!", 13); fclose($myfile); fopen("gfg.txt", "r"); echo fread($myfile, filesize("gfg.txt")); fclose($myfile); Output : Geeksforgeeks
以下示例程序旨在說明fputs()函數:
程序1
<?php
// Opening a file
$myfile = fopen("gfg.txt", "w");
// writing content to a file using fputs
echo fputs($myfile, "Geeksforgeeks is a portal of geeks!");
// closing the file
fclose($myfile);
?>
輸出:
35
程序2
<?php
// Opening a file
$myfile = fopen("gfg.txt", "w");
// writing content to a file with a specified string length using fputs
echo fputs($myfile, "Geeksforgeeks is a portal of geeks!", 13);
// closing the file
fclose($myfile);
//opening the same file to read its contents
fopen("gfg.txt", "r");
echo fread($myfile, filesize("gfg.txt"));
// closing the file
fclose($myfile);
?>
輸出:
Geeksforgeeks
參考:
http://php.net/manual/en/function.fputs.php
相關用法
- PHP each()用法及代碼示例
- p5.js day()用法及代碼示例
- p5.js second()用法及代碼示例
- p5.js int()用法及代碼示例
- PHP each()用法及代碼示例
- PHP exp()用法及代碼示例
- p5.js arc()用法及代碼示例
- d3.js d3.lab()用法及代碼示例
- d3.js d3.hcl()用法及代碼示例
- p5.js sq()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- CSS hsl()用法及代碼示例
注:本文由純淨天空篩選整理自Shubrodeep Banerjee大神的英文原創作品 PHP | fputs() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。