当用户单击提交时,我正在尝试创建一个文件。

Centos 7

PHP 7.2

我尝试用几种不同的方式修改代码,也尝试了file_put_contents似乎不起作用。

码: index.php

<form action="sendmail.php" method="post">
<input type="text" placeholder="fname" name="fname">
<button type="submit">submit</button>
</form>

sendmail.php:

<?php
$fname = $_POST["fname"];
echo "bef: " . $fname;
$myfile = fopen("/signupemails/" . $fname . ".txt", "w") or die("Unable to open file!");
echo "aft: " . $fname;
$txt = $fname;
fwrite($myfile, $txt);
fclose($myfile);
?>

目录创建文件

[root@webserver webdir]# ll -d /signupemails/
drwxrwxrwx. 2 apache apache 51 Aug 25 20:41 /signupemails/

如果我更改sendmail.php并对$fname进行硬编码并运行php sendmail.php,它将很好地创建文件

从浏览器创建它我得到"Unable to open file!"

分析解答

您的代码应该按原样工作,我刚刚对其进行了测试,并且看起来工作正常。

selinux被禁用了吗?