PHP 从网站写入文件不起作用


php writting to a file from a website not working

我试图使用 php 写入文件,但它看不到工作。

$file = fopen("test.txt","w");
    fwrite($file,"Hello World!");
   fclose("$file");

我使用学校服务器发布了该网站。 和 php 文件/文本文件都在public_html文件夹中。问题是,将其运行到终端会写入文件。将其作为网站运行不会。

我该如何解决这个问题?

附言我试过 chmod 644、777、755

删除fclose中的引号:

$file = fopen("test.txt","w");
fwrite($file,"Hello World!");
fclose($file);

另外,您必须授予所有权限:请参阅该答案。

嗯......它是UNIX/Linux服务器吗?您可能希望授予组www-data写入文件夹的权限。

假设您的 Web 文件夹/var/www,请尝试执行以下操作:

sudo chown `whoami`:www-data /var/www
sudo chmod 775 /var/www

再试一次。如果您在个人 Web 文件夹 (http://domain.ext/~yourusername) 中工作,则命令应为:

chown `whoami`:www-data ~/public_html
chmod 775 ~/public_html

现在运行脚本可以吗?