Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using Ubuntu 16.04 & xampp 5.6.30. I am trying to create a simple login program using file with validation but I am facing problem while putting my data into a file named "login.txt".

Error:

file_put_contents(login.txt): failed to open stream: Permission denied in /opt/lampp/htdocs/PHP/Practice/PHP Functions/Login Validation/login.php on line 38

My code:

$input = $userName.'|'.$userPassword.'\n';

$myfile = file_put_contents('login.txt', $input.PHP_EOL , FILE_APPEND | LOCK_EX);
like image 729
Animesh Avatar asked Mar 31 '17 15:03

Animesh


2 Answers

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.

like image 155
Attaullah Khan Avatar answered Sep 18 '22 12:09

Attaullah Khan


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

like image 35
Youmy001 Avatar answered Sep 16 '22 12:09

Youmy001