unable to create a file in php

April 14, 2016 8.4k views
LAMP Stack Fedora 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.

1 comment
  • 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.

3 Answers

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

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

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.

  • Man, you've just saved me lots of pain and sorrow. Thanks.

    I've had the same problem and started wondering if I'm living in an alternate universe, where the laws of logic cease to exist.

    So far this SELinux has been way more trouble than it's worth.

Have another answer? Share your knowledge.