我使用的是5.1.6版,我正在观察一个奇怪的问题。我无法从脚本创建和写入文件,而如果我显式创建文件然后运行脚本则会写入数据。
我在这里错过了一些明显的东西吗?
我正在尝试的测试代码是:
$message = "Test";
$myFile = "testFile.txt";
if (file_exists($myFile)) {
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
} else {
chmod("/path/to/dir/*", 0755); //updated code
$fh = fopen($myFile, 'w') or die("Cannot open file \"$myFile\"...\n");
fwrite($fh, $message) ;
}
fclose($fh);
结论: 感谢大家的回复。这是一个许可问题。我更改了目录路径,它可以工作:)
答案 0 :(得分:2)
我遇到了类似的问题,并通过将文件夹的所有者更改为 apache 用户来解决此问题。这应该为您的php脚本提供制作文件和写入该文件夹中的文件所需的权限。 我猜你将无法通过服务器访问(ssh或ftp)从php脚本中提取该文件夹。至少,那是我必须走的路。
答案 1 :(得分:1)
你的代码很好。只需要chmod所在的行。
注释掉chmod("/path/to/dir/*", 0755);
这将chmod设置文件夹中的所有文件。
请参阅chmod上的PHP手册 http://php.net/manual/en/function.chmod.php
$message = "Test";
$myFile = "testFile.txt";
if (file_exists($myFile)) {
$fh = fopen($myFile, 'a');
fwrite($fh, $message."\n");
} else {
//chmod("/path/to/dir/*", 0755);
$fh = fopen($myFile, 'w') or die("Cannot open file \"$myFile\"...\n");
fwrite($fh, $message) ;
}
fclose($fh);