Search code examples
phpcentos7fopen

php how to create file from browser


When a user clicks submit I am trying to create a file.

Centos 7

php 7.2

I have tried to modify the code a couple different ways also tried file_put_contents does not seem to work.

Code: 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);
?>

Directory to create file

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

If i change sendmail.php and hardcode the $fname and run php sendmail.php it will create the file fine

Creating it from the browser I get "Unable to open file!"


Solution

  • Your code should work as is, i just tested it and it seemed to work fine.

    Is selinux disabled?