failed to open stream: Permission denied in /opt/lampp/htdocs failed to open stream: Permission denied in /opt/lampp/htdocs linux linux

failed to open stream: Permission denied in /opt/lampp/htdocs


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.

https://linux.die.net/man/1/chmod


Permission errors can be solved using the following two methods.

Method 1:

Find web server user

ps aux | grep httpd

Note: It will be daemon by default if you haven't changed it.

Web Server Name

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.

  • First 7 in 777 is for User.
  • 2nd is for Group.
  • Last is for Others.

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.


This error is due to file permissions. The file or folder user does not have the permissions to write in the directory.

$ sudo chmod -R 755 /directory

755 – This set of permission is commonly used in web server. The owner has all the permissions to read, write and execute. Everyone else can only read and execute, but cannot make changes to the file.

https://www.maketecheasier.com/file-permissions-what-does-chmod-777-means/