PHP写文件

本文概述

PHP fwrite()和fputs()函数用于将数据写入文件。要将数据写入文件, 你需要使用w, r +, w +, x, x +, c或c +模式。

PHP写入文件-fwrite()

PHP fwrite()函数用于将字符串的内容写入文件。

句法

int fwrite ( resource $handle , string $string [, int $length ] )

例子

<?php
$fp = fopen('data.txt', 'w');//opens file in write-only mode
fwrite($fp, 'welcome ');
fwrite($fp, 'to php file write');
fclose($fp);

echo "File written successfully";
?>

输出:data.txt

welcome to php file write

PHP覆盖文件

如果再次运行上述代码, 它将删除文件的先前数据并写入新数据。让我们看看仅将新数据写入data.txt文件的代码。

<?php
$fp = fopen('data.txt', 'w');//opens file in write-only mode
fwrite($fp, 'hello');
fclose($fp);

echo "File written successfully";
?>

输出:data.txt

hello

PHP附加到文件

如果你使用一种模式, 它将不会删除文件的数据。它将数据写入文件的末尾。请访问下一页以查看将数据附加到文件中的示例。

点击我了解更多详细信息…

来源:

https://www.srcmini02.com/29432.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37941.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?