gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:25:53 24 4
gpt4 key购买 nike

我在 linux fedora 机器上安装了 apache 服务器我把下面的 test.php 和 test.html 放在 var/www/html但是当我在 firefox 上打开 127.0.0.1/test.htmltest.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

测试.php:

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

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

测试.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>

最佳答案

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

第一个让网络服务器拥有目录。第二个确保只有“apache”组中的用户可以读/写文件。它还表示机器上没有其他用户甚至无法阅读它们。

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

关于php file_put_contents 无法在 apache 服务器目录上创建文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13251815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com