在 PHP 中使用 fopen 时不能使用带“+”的任何内容
Can't use anything with "+" when using fopen in PHP
我想读取和写入 PHP
中的 .txt
文件,我已经可以使用
读取文件
$Readfile = fopen("file.txt", "r") or exit("Unable to open file!");
因为我想覆盖文件,我得到建议使用
$Readfile = fopen("file.txt", "r+") or exit("Unable to open file!");
但总是得到 "Unable to open file!" 我也尝试过使用 w+
之类的其他方法,但仍然没有给出任何结果。这是我的代码的一些更新
<?php
//$Readfile = fopen("pengumuman.txt","r") or exit("Unable to open file!");
$Readfile = file_get_contents("pengumuman.txt");
if (isset($_POST["btnSimpan"])) {
$pengumuman = $_POST["pengumuman"];
fwrite($Readfile, $pengumuman);
}
?>
<textarea class="span12" style="min-height: 200px; height: auto;" name="pengumuman">
<?php
while(!feof($Readfile)) {
echo fgets( $Readfile);
}
?>
我试过 file_get_contents
但它给了我一个很长的加载和空白文本区域
除了使用它,您可以尝试:
$Readfile = file_get_contents("file.txt");
PHP 可以更好地处理流程。稍后,如果您想写入文件,可以这样使用 file_put_contents
:
if (file_put_contents("file.txt", $Readfile . "Additional Content Here"))
echo "File written!";
else
echo "Error writing file!";
代码没有问题,因为我 运行 它在 linux 我需要更改权限。处理完所有事情后 运行 就应该如此。谢谢你。如果您遇到这种情况,请遵循此 link php can't write to file in linux
我想读取和写入 PHP
中的 .txt
文件,我已经可以使用
$Readfile = fopen("file.txt", "r") or exit("Unable to open file!");
因为我想覆盖文件,我得到建议使用
$Readfile = fopen("file.txt", "r+") or exit("Unable to open file!");
但总是得到 "Unable to open file!" 我也尝试过使用 w+
之类的其他方法,但仍然没有给出任何结果。这是我的代码的一些更新
<?php
//$Readfile = fopen("pengumuman.txt","r") or exit("Unable to open file!");
$Readfile = file_get_contents("pengumuman.txt");
if (isset($_POST["btnSimpan"])) {
$pengumuman = $_POST["pengumuman"];
fwrite($Readfile, $pengumuman);
}
?>
<textarea class="span12" style="min-height: 200px; height: auto;" name="pengumuman">
<?php
while(!feof($Readfile)) {
echo fgets( $Readfile);
}
?>
我试过 file_get_contents
但它给了我一个很长的加载和空白文本区域
除了使用它,您可以尝试:
$Readfile = file_get_contents("file.txt");
PHP 可以更好地处理流程。稍后,如果您想写入文件,可以这样使用 file_put_contents
:
if (file_put_contents("file.txt", $Readfile . "Additional Content Here"))
echo "File written!";
else
echo "Error writing file!";
代码没有问题,因为我 运行 它在 linux 我需要更改权限。处理完所有事情后 运行 就应该如此。谢谢你。如果您遇到这种情况,请遵循此 link php can't write to file in linux