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);
Permission errors can be solved using the following two methods.
ps aux | grep httpd
Note: It will be daemon by default if you haven't changed it.
Web Server Name
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.
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.
User
.Group
.Others
.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.
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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With