我无法使用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);
?>
文件夹中都存在这两个文件。我在bob.txt上看不到任何更新。
这是Windows中的权限错误吗?它在家里的笔记本电脑上工作正常。我也无法使用filezilla更改我网站上的php文件。
答案 0 :(得分:1)
很可能是文件权限问题。
使用以下代码,使用指向文件的直接指针而不是路径来尝试代码:
我添加了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);
?>
答案 1 :(得分:0)
也许您必须将bob.txt的权限设置为0777(或其他内容)。在FileZilla中,它非常简单,因为您只需检查所需的权限即可。