我的代码是
<?php
$fp = fopen('test.txt', "a+");
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
但是test.txt仍然是空的。我没有看到任何错误,我也不明白为什么不写作。
答案 0 :(得分:3)
这是文件权限问题。
这会将您的文件chmod到777
,使其可写。
在Linux服务器上测试:
<?php
$fp = fopen('test.txt', "a+");
chmod("test.txt", 0777); // try also 0666 or 0644
fwrite($fp, 'Cats chase mice');
fclose($fp);
?>
您还可以使用0666
或0644
,具体取决于您希望文件拥有多少权限,但0644
将是您最安全的选择;见下文。
您还可以通过FTP输入chmod 777 test.txt
或chmod 666 test.txt
或chmod 644 test.txt
。
我的服务器让我使用0644
来编写和附加,并且可能在您想要执行代码的服务器上有所不同。
引自http://www.centos.org/docs/2/rhl-gsg-en-7.2/s1-navigating-chmodnum.html
当心666和777
将权限设置为666或777将允许每个人读取和写入文件或目录。
这些权限可能允许篡改敏感文件,因此通常,使用这些设置不是一个好主意。
以下列出了一些常见设置,数值及其含义:
-rw------- (600) — Only the owner has read and write permissions.
-rw-r--r-- (644) — Only the owner has read and write permissions; the group and others can read only.
-rwx------ (700) — Only the owner has read, write and execute permissions.
-rwxr-xr-x (755) — The owner has read, write and execute permissions; the group and others can only read and execute.
-rwx--x--x (711) — The owner has read, write and execute permissions; the group and others can only execute.
-rw-rw-rw- (666) — Everyone can read and write to the file. (Be careful with these permissions.)
-rwxrwxrwx (777) — Everyone can read, write and execute. (Again, this permissions setting can be hazardous.)
以下是目录的一些常见设置:
drwx------ (700) — Only the user can read, write in this directory.
drwxr-xr-x (755) — Everyone can read the directory, but its contents can only be changed by the user.
答案 1 :(得分:2)
在控制台中写下
chmod a+w test.txt