【问题标题】:Trouble with writing to file with php and my home server使用 php 和我的家庭服务器写入文件时遇到问题
【发布时间】:2013-01-06 00:56:13
【问题描述】:

所以,我有这个简单的小 php 脚本。它可以正常运行和编译,并且在我编写它的机器上按照我想要的方式工作。我在运行 Debian 6.0.6、32 位的个人家庭网络服务器上运行它。它是带有 php 的 apache。而且我知道 php 正在服务器上运行。

    <?php
    $hitsfile = "hits.txt";                                                 #name of file
    $filehandle = fopen($hitsfile, 'r') or die ("Couldn't read file.");     #Opens file, 'hitsfile' to be read.
    $hits = fread($filehandle, 5);                                          #reads file to the introduced variable, 'hits'
    fclose($filehandle);                                                    #closes file
    $hits++;                                                                #increments the variable that it read.
    $filehandle = fopen($hitsfile, 'w') or die ("Couldn't write to file."); #opens file to be read. 
    fwrite($filehandle, $hits);                                             #writes the hits variable to file.
    fclose($filehandle);                                                    #closes file.
    echo $hits;                                                             #outputs the hits variable.
    ?>

当我通过网络浏览器从服务器访问文件时,我收到“无法写入文件”。错误。那么,它会正确打开文件并读取它。当它打开它进行写入时,它会失败。我假设这是权限或其他方面的某种问题。我对如何解决这个问题有点茫然。有任何想法吗?协助将不胜感激!我已经google了几天了,我无法解决这个问题。我是一个 php 'noob',我对运行基于 linux 的网络服务器非常陌生,但是,嘿,你必须以某种方式学习。 :*l

【问题讨论】:

  • 确保文件权限允许写入文件并且它不是只读的。
  • var_dump(is_writable($hitsfile)); 告诉你什么?

标签: php apache webserver debian lamp


【解决方案1】:

试图检查文件的权限? Linux 文件系统有一个非常严格的权限系统。在终端上写:

ls -la /path/to/my/file.txt

这将授予您对左列的权限。请阅读this article 确定,并检查Apache 是否具有该文件的“写入”权限。如果没有,请使用chmod 命令授予 Apache 对该文件的访问权限(或 chown 命令,将此文件的所有者更改为 apache,如果此文件的所有者具有写入权限)。

【讨论】:

  • 啊啊啊啊啊啊啊啊。哈哈哈。我爱你。
  • :) 如果此答案对您有所帮助,请检查“V”以将我标记为正确答案。谢谢:)
猜你喜欢
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-04
  • 2021-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多