Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What permissions are required to allow Apache-PHP write to a folder?

Tags:

php

apache

rhel7

I am running RHEL 7 with PHP 5.4, Apache 2, which requires PHP-FPM.

I created a group called WWW and added the Apache user to it. All is working fine for PHP.

I now require to write some files to a directory called "reports".

Reports has the following permissions

drwxrwsr-x.  2 ec2-user www        6 Aug 17 13:23 reports

When I use the following PHP code to write a file to "reports" I get a permissions error

$handle = fopen('text.xls', 'w+');
{
   if (!fwrite($handle, $content))
   die("cant' write");
}

What are the correct permissions I need to use on the "reports" directory?

like image 684
user813813 Avatar asked Mar 18 '26 00:03

user813813


1 Answers

In addition to the file permissions mentioned, you also need to make sure you aren't running into a permissions issue with SELinux; it's running by default on RHEL 7.

You can use ls -lZ <location> to see what the current SELinux contexts are for a given file or directory.

By default, /var/www/html has the httpd_sys_content_t context, which will prevent httpd from writing to that directory. In order to allow httpd to write to a directory, you need to tell SELinux to allow it by giving it the httpd_sys_rw_content_t context, which can be done with these commands.

sudo semanage fcontext -a -t httpd_sys_rw_content_t <location>
sudo restorecon -v <location>

Be sure to replace <location> with the actual location of your "reports" directory.

like image 162
Dave Mulford Avatar answered Mar 19 '26 15:03

Dave Mulford



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!