【问题标题】:FIle doesn't get updated by appending text in PHP文件不会通过在 PHP 中附加文本来更新
【发布时间】:2020-11-10 10:16:42
【问题描述】:
我正在尝试将带有 PHP 的文本附加到我的文本文件中,但代码执行后123.txt 仍然为空。
代码:
<?php
$fp = fopen('123.txt', "a+");
fwrite($fp, 'Cats chase dogs');
fclose($fp);
?>
我没有看到任何问题,我不明白为什么它不写。
我们将不胜感激任何形式的帮助。
【问题讨论】:
标签:
php
file
append
fwrite
【解决方案1】:
在使用文件处理程序写入文件之前,您可以检查它是否可写或存在。
<?php
$file = '123.txt';
if( file_exists( $file ) ){
if( is_writable( $file )){
$fp = fopen( $file, "a+");
fwrite($fp, 'Cats chase dogs');
fclose($fp);
}else{
echo 'file is not writable, check permission';
}
}else{
echo 'file does not exist';
}
?>
另外,如果你在开发中,请从 php.ini 文件中打开 php 的错误报告模式以了解错误。