【问题标题】:php writting to a file from a website not workingphp从网站写入文件不起作用
【发布时间】:2013-11-28 07:19:39
【问题描述】:

我正在尝试使用 php 写入文件,但它无法正常工作。

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

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

我该如何解决这个问题?

附:我试过 chmod 644, 777, 755

【问题讨论】:

  • chmod 777 应该可以解决问题。
  • 那么当作为网页访问时允许运行的其余代码在哪里?你的文件不能只有这三行。
  • 不行,不行:(

标签: php html


【解决方案1】:

删除fclose 中的引号:

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

另外,您必须授予所有权限:请参阅that answer

【讨论】:

    【解决方案2】:

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

    假设您的网络文件夹是/var/www,请尝试这样做:

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

    然后再试一次。 如果您在您的个人网络文件夹 (http://domain.ext/~yourusername) 中工作,那么命令应该是:

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

    现在可以运行脚本了吗?

    【讨论】:

      猜你喜欢
      • 2016-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      相关资源
      最近更新 更多