- PHP: fopen() Permission denied
- 10 Answers 10
- failed to open stream: Permission denied in /opt/lampp/htdocs
- 5 Answers 5
- Method 1:
- Find web server user
- In my case it’s daemon, replace it with yours in following command.
- Method 2:
- Give All permissions to that file.
- Alternative command:
- Advice:
- Centos 7 / Apache / PHP — mkdir(): Permission denied
- Permission denied on PHP as a shell script
- 5 Answers 5
PHP: fopen() Permission denied
where is the problem? Thanks a lot!I have found the problem,I use FC13,because of the protect of SELinux,some action is denied.So, I just need to get rid of the protect.
Is php running as jt user? If the file you want to open is owned by some other user, that might be a problem, too.
oh. I found the problem. I use FC13,because of the protect of SELinux, some action is denied. Thanks! 🙂
10 Answers 10
This issue can also be a result of having SELinux enabled. This can be solved using:
chown -R apache:apache /var/www/html/directory_to_write chcon -R -t httpd_sys_content_t /var/www/html/directory_to_write chcon -R -t httpd_sys_rw_content_t /var/www/html/directory_to_write
is most likely looking in another directory
I have been receiving the same error «failed to open stream. permission denied»when trying to write a file on the server using PHP. I tried everything on the internet to fix the error. I changed ownership of the files,directories and sub-directories on the server to «apache», I did a chmod 777 on all the files, directories, sub-directories, I ran restorecon -R , I ran chcon unconfined_u:object_r:httpd_user_content_t:s0 on all the files, but the only thing that seemed to work is turning SELinux off completely.
I finally resolved the issue. The problem lay in the boolean parameters used by SELinux. I performed the following command to get a list of all the booleans related to httpd.
This gave a list of about 36 parameters.
I painfully went and turned on every boolean using the setsebool command until the «failed to open stream. permission denied» error went away.
When I turned «on» the httpd_unified boolean, the error went away!! When I turned it «off», the error came back!!
failed to open stream: Permission denied in /opt/lampp/htdocs
That’s a permission denied error. are you saving the login.txt file inside of the /opt/lampp/htdocs/PHP/Practice/PHP Functions/Login Validation/ folder. if not, then run this command stat theFolder and post the output.
5 Answers 5
This error is due to file permissions. The php/apache user does not have the permissions to write in the directory. One way of fixing that is to change the mod byte of the directory with the command chmod . You need super permissions to execute this modification if your session user does not own the directory.
$ sudo chmod -R 777 /path/to/directory
The mod 777 set read/write and execution to directory for every users on the system. The R option applies recursively the modification to every files and sub-directories. So, you can change the permission of all the contents of your htdocs directory all at once.
change permission to 777 is bad idea, because it gives permission to anybody to read and write into directory. In Ubuntu, apache user usualy www-data (uid=33). Change the owner of directory to this user with: chown -R www-data /path/to/directory
Permission errors can be solved using the following two methods.
Method 1:
Find web server user
Note: It will be daemon by default if you haven’t changed it.
In my case it’s daemon, replace it with yours in following command.
sudo chown -R daemon /opt/lampp/htdocs/[Working Project]/
As in Linux chown is used to change ownership of any folder or file. So this command gives ownership of that folder to the daemon user, which will enable the daemon user to have complete access to that folder and everything inside it too.
Method 2:
Give All permissions to that file.
The following command gives Read , Write and Execute permissions to User , Group and Owner . So User is you, who can also modify it, other programs such as text editors can also edit them, and so PHP too.
sudo chmod 777 /opt/lampp/htdocs/[Working Project]/File.txt
Here permissions are provided using numeric number
- 7 is for all permissions read , write and execute .
- 6 is used for read and write .
- 5 is used for read and execute .
- 4 is used for read only.
The permissions are granted in ugo pattern.
Alternative command:
sudo chmod u+rwx,g+rwx,o+rwx /opt/lampp/htdocs/[Working Project]/File.txt
Here it is more clear that which permissions you’re assigning to whom. so u stands for User, and is being assigned Read(r), Write(w) and Execute(e) permissions, similarly g is for Group, and o is Other.
Advice:
Method 2 is not suitable for most of the scenarios. I recommend using the First Method because method 2 will only give read, write and execute permissions to a file where the file must be created by you manually first. What if you wanted php to create the file according to your logic in the website. In that case this method will not work. if daemon will be the owner, it can do that only.
Centos 7 / Apache / PHP — mkdir(): Permission denied
Before you yell DUPLICATE! and banish me into the abyss, hear me out. 🙂 I’ve been developing an intranet site for the company I work with, and originally I was developing it directly from my workstation running Windows 7 / IIS. As I’ve neared completion of the site/applications, I was given a Centos 7 box that’s running Apache, so I’ve begun the process of migrating it over to that system. I’ve sorted out most of the minor issues, and got the site running now. However, there is a portion of the site that interacts with our database and creates a log directory/file based upon the person that’s logged in. This portion utilizes the mkdir() function, but I’m getting permission issues with it.
- Since I’m using Centos 7, the Apache user/group is apache:apache. I’ve utilized chown and changed the owner of /var/www/html/ and the Logs folder to the apache user/group. To be on the safe side, I double checked the httpd.conf and also checked the running processes to ensure I had the correct user/group.
- I’ve attempted, just for testing purposes, to change the permissions of all of those folders to chmod 777, but getting the same error.
- Searched StackOverflow from top to bottom, and only getting answers for things that I’ve already tried, but to no avail.
So whether I use chmod or chown to change the permissions for the folders being accessed, I’m getting the same type of error. To be on the safe side, I checked the PHP user also, and it’s using the apache user as well.
If anyone may have additional insight as to why it isn’t working, even with the permissions changed, then please enlighten me. If I happened to miss the one article that explained this particular situation, then yell duplicate and banish me to the abyss. lol 🙂
EDIT Okay, so upon further testing, I’ve discovered 2 issues that seem to be causing this overall issue.
- PHP is trying to set permissions for the folder when it’s created from the script, and it seems the server doesn’t like that even if apache is the owner. Upon removing that portion of the code, the permissions error goes away, and I get an error with «no such file or directory.»
- When testing creating directories with the user Apache through sudo, it was able to make a directory without a problem. However, my PHP script is creating 2 directories, and then a log file in the last one created. It seems that you can’t create two directories at the same time?? For example, there is a Logs folder already created, and PHP is trying to create two directories under it, one for the user’s ID and then another folder inside of that one with the date. So once the PHP script runs, it should create something like «Logs/5235/3-3-2015/»
Just to note, I’ve attempted to set the recursive value to true on mkdir in PHP, but that’s when I get the permission issues originally noted. When recursive/mode is removed, it doesn’t get the permission issue, but it isn’t able to create nested directories.
To test my theories, I removed the nesting and tried to make mkdir create just 1 directory, and it’s generating the same errors as before. Although it’s owned by Apache, and even if I set it to 777, it throws back permission issues.
Permission denied on PHP as a shell script
What happens after you chmod 0755 vardump ? Right now, permissions on it are 777, and that’s an itsy bit dangerous as root.
5 Answers 5
Solved it. Based on all your answers, I suspected the problem is not in php at all. I’m running the script on a NTFS mount so I tried to move it to the root mount and it worked there. So I added exec to /etc/fstab and it worked.
UUID=0B02861D7B0D6A31 /media/Data ntfs-3g defaults,users,exec,locale=en_PH.UTF-8 0 0
This answer also gave me the clue for it. Thanks a lot for all your help!
As a workaround, it’s probably worth mentioning that simply running php myfile.php should mostly work as expected.
Are you possibly using two different php instances? Run the following, is it something other than /usr/bin/php?
Do you have permission to run /etc/alternatives/php on the system? t’s possible that when you run the scripts with php vardump it’s using a different php binary than /etc/alternatives/php. To check that, run which php and see what it prints out. Also, what’s the output of ls -l /etc/alternatives/php .
Known that which php is /usr/bin/php it’s possibly something to do with the php installation as Marco mentioned
I would run which php — I know on Ubuntu that php is linked to /usr/bin/php (tested on Ubuntu 10.04 Desktop and Server your script runs fine with both 0777 and 0755 permissions on my machine using the following:
marco@FW2X9K1:~/Projects$ php -v PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:03:45) Copyright (c) 1997-2009 The PHP Group Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies marco@FW2X9K1:~/Projects$ ls -lah vardump -rwxrwxrwx 1 marco marco 41 2010-06-22 20:17 vardump marco@FW2X9K1:~/Projects$ ls -l /usr/bin/php lrwxrwxrwx 1 root root 21 2010-05-26 09:15 /usr/bin/php -> /etc/alternatives/php marco@FW2X9K1:~/Projects$ ls -l /etc/alternatives/php lrwxrwxrwx 1 root root 13 2010-05-26 09:15 /etc/alternatives/php -> /usr/bin/php5 marco@FW2X9K1:~/Projects$ ls -l /usr/bin/php5 -rwxr-xr-x 1 root root 7836792 2010-05-13 16:20 /usr/bin/php5 marco@FW2X9K1:~/Projects$ ./vardump array(1) < [0]=>string(9) "./vardump" >
This likely will need to be posted in serverfault. Lastly run the following to ensure everything is installed correctly.
marco@FW2X9K1:~/Projects$ dpkg -l | grep php5 ii libapache2-mod-php5 5.3.2-1ubuntu4.2 server-side, HTML-embedded scripting languag ii php5 5.3.2-1ubuntu4.2 server-side, HTML-embedded scripting languag ii php5-cgi 5.3.2-1ubuntu4.2 server-side, HTML-embedded scripting languag ii php5-cli 5.3.2-1ubuntu4.2 command-line interpreter for the php5 script ii php5-common 5.3.2-1ubuntu4.2 Common files for packages built from the php ii php5-dev 5.3.2-1ubuntu4.2 Files for PHP5 module development ii php5-mysql 5.3.2-1ubuntu4.2 MySQL module for php5