apache php file_put_contents permission denied - php

The php script is unable to create file owing to denial of permissions.
PHP Script:
<?
echo exec('whoami');
file_put_contents('/var/www/html/sample.txt','Some random Text');
?>
Output :
apache
**Warning**: file_put_contents(/var/www/html/test/samds.txt): failed to open stream: Permission denied in **/var/www/html/index.php** on line **3**
The directory permissions are all 777

I have had same issue and i misplaced some configuration.Firstly remeber that the user who created the php file can read the file and if the user who created php file is different than the apache user in your case it is apache in that case you will get this exception if you try to write from different users.So what you have to do is simply,and i would suggest you to revoke the 777 permissions as i do not like idea of your files being publicity available on read and write to the world,so get it back to 755 and do the next on the command line of your server.
cd /var/www
change the user of the HTML only as we need it to be writable only,so do not touch the var and www,so basically as soon as you have navigated to he www directory you are going to change the HTML directory owner to the one who is currently running the apache server,and you got that info already,in your case it is apache so we are going to do the next,
chown apache:apache html
At this stage the html file owner will be apache so you will be able to write onto it.Anyway and meanwhile i would strongly suggest you to create separate directory inside html lets say testDir and try there,i would not suggest to change the root folder permissions,i had bad experience with them.Anyway i hope it will help as i suffer more then hour and just now got the right way to do it.This got my issue fixed,hope it will fix your's too.!

Related

PHP can't create file, permission denied

I am trying to run some PHP applications on CentOS powered server with apache and MySQL. My apps have to create files on server, but it always says that permission is denied to create a file.
Files are located in /var/www/html. I even tried setting 777 permission to html folder and html/*. I changed apache user and group to myuser, that exists, and restarted apache. I changed the ownership of html folder and all files inside to myuser. I even tried changing document root to /home/myuser/public_html
I tried this code to test write permission. File location is /var/www/html/index.php and /home/myuser/public_html/index.php
$handle = fopen("a.txt", "w");
fwrite($handle, "test");
fclose($handle);
I am just more than amazed by this problem. The same configuration works on my another Ubuntu server.
Some geniuses must be here, help me.
You need to allow you server user to do write operation on your directory, User the below command, If it is a multilevel directory use -R flag.
sudo chown www-data my-dir
Instead of manually giving permission you can try chmod($handle, 0777) within your code.

Ubuntu 16.04 Permission Denied

index.php is located at /var/www/html/
It tries to include autoload.php from /root/vendor
And I'm getting include_once(../../../root/vendor/autoload.php): failed to open stream: Permission denied
Ubuntu 16.04
Enter your terminal and run this command in your terminal.
sudo nautilus
This command will enter you as root into the files application and mostly allow you to do anything to the files that you need to change.
go to the file that you want to change permissions on, mostly visit the folder var, then the folder www, then the folder html.
Once you are here, right click the index.php file and click properties, or permissions.
Change the permissions to read and write or create & delete for all of the options.
I hope I was able to help and much clearer this time, as I have received a downvote for not being clear enough.

PHP Write file - Permission Denied

I can't seem to get PHP to write to a text file on my Apache web server.
The file itself (log.txt) has an owner of "apache:apache" with access "-rw-rw-r--", its folder is owned by "apache:apache" and has permissions "drwxrwxr-x"...
Every time I tried to access log.txt, the Apache log reports "Permission Denied" on that file name.
Can anyone help?
Thanks.
apache usually runs as user www-data:www-data not apache:apache.
Are you sure the right uid/gid?
Try writing to a new file and see who owns it

Warning: file_put_contents(): failed to open stream: Permission denied

Hi I am new to Mac and web development..
These days I am trying to do a simple web page with a simple form with HTML and Javascript, and the form will be processed by PHP. The PHP will be access txt file, read from and write to this txt file.
The error message comes from this line of code.
file_put_contents("order.txt", $content);
and the error message is
Warning: file_put_contents(order.txt): failed to open stream: Permission denied in /Users/maoyang/Sites/3006/process.php on line 57
all the HTML and PHP codes are in the myname/Sites/3006 folder of my computer. I have done some research and all of them tells me that I need to change some configuration to make the txt file writeable, but really very confused how to do so. Anyone know a good instruction page I could follow(for mac environment).
Thank you very much..
On OSX Apache is run by the _www user. You need to make sure that this user has permissions to write on the file you want to modify. To do so, open a terminal window and type:
sudo chown -R _www:_www /Users/maoyang/Sites/3006
Fill in your Administrator user password when requested. This will change the ownership of the /Users/maoyang/Sites/3006 folder to the _www user (the same one that run Apache) so the webserver will have writing permission on that folder (and all subfolders and files)

test.php cannot output file into Leopard filesystem

may i know what permission do i need to add, to allow test.php to able to write file into macos filesystem
PHP Error: <br />
<b>Warning</b>: file_put_contents(20090915203127.jpg) [<a href='function.file-put-contents'>function.file-put-contents</a>]: failed to open stream: Permission denied in <b>/Library/WebServer/Documents/testphp/test.php</b> on line <b>8</b><br />
ERROR: Failed to write data to 20090915203127.jpg, check permissions
this is the error i get on leopard, i already chmod ugo+x for test.php
It's not the file that needs permissions, it's the process running PHP. Assuming you're using (Snow) Leopard in it's default state, you'll need to give the _www user write permissions to the given directory, or give the world write access (a less savoury idea). Both of these can be accomplished with a combination of chown and chmod.
You need to make sure the user the script is running under is allowed to write to the file trying to write to (i.e. 20090915203127.jpg, not the PHP script). If 20090915203127.jpg does not exist, you'll need to make sure the directory that file's going to is writable (by default this will be the present working directory).
You may need to read about chmod.
As far as I know you need root rights to be able to write directly to /Library. Is it absolutely necessary to write to this directory?
If you just write files to your own account, under /Users/accountname it should work (although that depends on the user who is executing the php script).
You need to make the target file / directory writable to the web server. test.php is not into that...

Categories