php file_put_contents无法在apache服务器目录上创建文件

时间:2012-11-06 13:12:11

标签: php javascript

我在linux fedora机器上安装了apache服务器 我在var / www / html上放了以下test.php和test.html 但是当我在firefox上打开127.0.0.1/test.html时 test.php不会创建text.txt文件,更不用说将字符串写入文件了 并且“echo $ var”

也没有输出

错误是

Warning: file_put_contents(test.txt): failed to open stream: Permission denied in /var/www/html/getdata.php on line 7

目录的权限是:

drwxr-xr-x. 2 root root 4096 Nov  6 14:14 html

test.php的:

<?php
$v="x";
$fname='test.txt';
$rv=file_put_contents($fname,$v);
echo $rv;
echo $v;
?>

test.html是如此复杂,因为我打算在服务器上写一些复杂的文件,但由于存在一些问题,我简化了test.php

的test.html:

<!DOCTYPE html>
<html>
<body>

<form id="yourFormID" method="POST" action="/getdata.php" ></form>

<script>
  function sendArray( theArray )
  {
    var frm = document.getElementById('yourFormID');
    fld = document.createElement("INPUT");
    fld.name ="data"; 
    fld.type = "hidden";
    fld.value = JSON.stringify(theArray);
    frm.appendChild(fld);  
    frm.submit();
   }

   var yourArray = [0.000023323,0.00001292,0.00003323];

    sendArray( yourArray );

    </script>
    </body>
    </html>

2 个答案:

答案 0 :(得分:2)

这是Linux的权限问题。尝试:

chmod 777 path/to/test.txt

在命令行。

编辑:这是一篇关于Linux文件权限的精彩文章。 http://www.tuxfiles.org/linuxhelp/filepermissions.html

编辑2:我可能会添加,为文件设置适当的权限是PHP可以使用file_put_contentsfwrite等操作所述文件的唯一方法。

答案 1 :(得分:2)

html目录目前由root拥有,但在Fedora下,Web服务器作为“apache”用户运行。 (参见https://fedoraproject.org/wiki/Administration_Guide_Draft/Apache?rd=Docs/Drafts/AGBeta/Apache)的“Apache文件安全性”部分

因此,作为root,请执行:

 chown -R apache:apache /var/www/html/
 chmod -R 770 /var/www/html

第一个使Web服务器拥有该目录。第二个确保只有“apache”组中的用户才能读/写文件。它还说机器上没有其他用户甚至可以阅读它们。

如果您需要其他用户才能将文件写入您的网页树,请将其添加到“apache”组。

相关问题