PHP教程
PHP Mysql
PHP面向对象
PHP常用

PHP 写入文件

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 Overwriting File

如果你再次运行上面的代码,它会擦除​​文件之前的数据并写入新的数据。让我们看看只将新数据写入 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 Append to File

如果你使用a模式,它不会删除文件的数据。它将在文件末尾写入数据。访问下一页以查看将数据附加到文件中的示例。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4