Question

unable to create a file in php

  • Fedora 23 x64
  • php 5.6.17

Permissions are as open as I know how to make them (777). Yes, I know this isn’t safe. Once I figure out the problem, I will make them safe.

# ls -lhd /srv/www/html/sigs/
drwxrwsrwx. 2 root root 4.0K Apr 13 19:37 /srv/www/html/sigs/

trial.php

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$file = '/srv/www/html/sigs/file.txt';
$mf = fopen($file, 'w');
fwrite($mf, 'hi');
fclose($mf);
echo $file;
?>

output

Warning: fopen(/srv/www/html/sigs/file.txt): failed to open stream: Permission denied in /var/www/html/trial.php on line 6

Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/trial.php on line 7

Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/trial.php on line 8
/srv/www/html/sigs/file.txt

I am pulling out my hair here :) I have tried setting the directory permissions to 777, 775, 755 I have tried setting the user and group to apache If I write to the /tmp directory, I don’t get any errors, but I also don’t get a file.

Subscribe
Share

It sounds like you’ve taken some good troubleshooting steps so far. The biggest one here, which you mentioned, is setting the ownership to apache. I would recommend changing this back to apache as it is currently owned by root according to the output you provided.

chown -Rf apache:apache /srv/www/html/sigs

Otherwise I am not seeing any other reason the write should fail.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Spent a lot of time looking for this answer but it’s all in bits and pieces and no one every posts a solution (well most of the time) so here is my solution and it’s used on various web control panels as well.

install and use MOD_RUID2

Install PHP with CLI (this is standard on newer versions)

In your HTTPD.CONF file in the virtual hosts, you’ll add the following, replacing username with the user’s login name, and usergroup with the user’s group (These are usually the same)


<IfModule !mod_ruid2.c>
    SuexecUserGroup username usergroup
</IfModule>
<IfModule mod_ruid2.c>
    RMode config
    RUidGid username usergroup
    RGroups @none
</IfModule>

An example of a Virtual host conf is:

<VirtualHost *:443>
  DocumentRoot "/home/imtheuser/public_html"
  ServerName imtheuser.com
  <IfModule !mod_ruid2.c>
    SuexecUserGroup imtheuser imtheuser
  </IfModule>
  <IfModule mod_ruid2.c>
    RMode config
    RUidGid imtheuser imtheuser
    RGroups @none
  </IfModule>
  <Directory "/home/imtheuser/public_html">
    allow from all
    Options None
    Require all granted
  </Directory> 
</VirtualHost>

This will allow apache/php to write to a directory owned by the user. It’s much safer then setting your chmod to 0777.

I solved a minute ago. SELinux has authority on writing permits. Please read accepted answer on this post : https://unix.stackexchange.com/questions/50639/httpd-cant-write-to-folder-file-because-of-selinux

Try this :

chcon -R -t httpd_sys_rw_content_t /path

write your target /path where you want to give write permission. I don’t know the security hole risk on this commands. Play with your own risk. Good day.

Exact same problem i have. Any solution offer? I think SELinux has controlled this. But not tested. Looking forward for any solution.

Excellent point ryanpq. Thank you.

Verify web user name

# ps aux | grep apache
apache   17856  0.0  2.6 457444 26508 ?        Sl   11:24   0:04 /usr/sbin/httpd -DFOREGROUND
apache   17893  0.0  2.8 459780 29156 ?        Sl   11:25   0:01 /usr/sbin/httpd -DFOREGROUND
...

# ps aux | grep httpd
apache   17856  0.0  2.6 457444 26508 ?        Sl   11:24   0:04 /usr/sbin/httpd -DFOREGROUND
apache   17893  0.0  2.8 459780 29156 ?        Sl   11:25   0:01 /usr/sbin/httpd -DFOREGROUND
...

Verify owner/permissions for directory

# ls -lhd sigs
drwxr-sr-x. 2 apache apache 4.0K Apr 14 09:29 sigs

To eliminate a path issue, I moved trial.php into the sigs directory and updated the $file variable so it is attempting to write directly within that directory

# cat /var/www/html/sigs/trial.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$file = 'file.txt';
echo "file: $file<bR>";
$mf = fopen($file, 'w');
fwrite($mf, 'hi');
fclose($mf);
?>

Output of accessing the file: http://xxxxxx/sigs/trial.php

file: file.txt

Warning: fopen(file.txt): failed to open stream: Permission denied in /var/www/html/sigs/trial.php on line 7

Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/sigs/trial.php on line 8

Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/sigs/trial.php on line 9

Change permissions for full access

# chmod 0777 /var/www/html/sigs
# ls -lhd /var/www/html/sigs/
drwxrwsrwx. 2 apache apache 4.0K Apr 14 09:29 /var/www/html/sigs/

And try again: http://xxxxx/sigs/trial.php

file: file.txt

Warning: fopen(file.txt): failed to open stream: Permission denied in /var/www/html/sigs/trial.php on line 7

Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/sigs/trial.php on line 8

Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/sigs/trial.php on line 9