unable to create a file in php

April 14, 2016 3.3k 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.

1 Answer

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
Have another answer? Share your knowledge.