PHP文件写入显示空屏幕

时间:2015-04-09 06:52:17

标签: php

我正在尝试编写一些内容infile,但它显示的是空白屏幕

$write = fopen("newfile.txt", "r");
fwrite($write, "John Doe");

这是我的代码,问题是什么,

2 个答案:

答案 0 :(得分:1)

使用

$fh = fopen($myFile, 'a')

示例

$myFile = "newfile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "John Doe 1\n";
fwrite($fh, $stringData);
$stringData = "John Doe 2\n";
fwrite($fh, $stringData);
fclose($fh);

输出

Floppy Jalopy
Pointy Pinto
John Doe 1
John Doe 2

答案 1 :(得分:0)

你的行动有错误

将'r'更改为'w'

$write = fopen("newfile.txt", "w");
fwrite($write, "John Doe");

但我建议你处理die

<?php
$write = fopen("newfile.txt", "w") or die("Unable to open file!");
fwrite($write, "John Doe");
fclose($write);
?>

更新:由于提问者希望附加旧内容

然后您需要在a

中设置fopen
<?php
$write = fopen("newfile.txt", "a") or die("Unable to open file!");
fwrite($write, "John Doe");
fclose($write);
?>

注意:最好在内容结尾处使用\n,因为它会破坏该行并将其带到下一行,否则内容将继续前一行本身

我建议你阅读php fopen