我无法使用php更新任何txt文件。当我编写如下所示的简单代码时:
<?php
// create file pointer
$fp = fopen("C:/Users/jj/bob.txt", 'w') or die('Could not open file, or fike does not exist and failed to create.');
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("C:/Users/jj/bob.txt");
// close file
fclose($fp);
?>
这两个文件都存在于文件夹中。我只是看不到鲍勃.txt的任何更新。
这是窗口中的权限错误吗?它在家里的笔记本电脑上工作正常。我也无法使用filezilla更改我网站上的php文件。
这很可能是文件权限问题。
使用以下代码尝试使用指向文件的直接指针而不是指向文件的路径来编写代码:
我添加了一个chmod
指令。请参阅上面的评论chmod ($file, 0644);
在我的托管WWW网站上成功测试:
<?php
// create file pointer
$file = "bob.txt";
$fp = fopen($file, 'w') or die('Could not open file, or fike does not exist and failed to create.');
// chmod ($file, 0777); // or use 0777 if 0644 does not work
chmod ($file, 0644);
$mytext = '<b>hi. This is my test</b>';
// write text to file
fwrite($fp, $mytext) or die('Could not write to file.');
$content = file("bob.txt");
// close file
fclose($fp);
?>
也许您必须将权限从 bob.txt 设置为 0777(或其他内容)。在FileZilla中,这非常简单,因为您只需检查所需的权限即可。