file_put_contents doesn't work when accessed in browser - php

I use php5.6 and Apache2.4.6 in centOS7.5
I put a php file in /var/www/html
like this :
<?php
echo (__DIR__ . "/test.text");
file_put_contents(__DIR__ . "/test.text","test text");
when I access this file through web browser(google chrome), file_put_contents do not function.
but,when I execute this file in console, file_put_contents do function. it make test.text file.
Is this problem due to server setting? or some php function doesn't work in access by a browser?
this is httpd/error_log
[Tue Oct 30 13:35:13.191011 2018] [:error] [pid 15037] [client XX.XXX.XX.XXX:29220] PHP Warning:
file_put_contents(/var/www/html/test.text): failed to open stream: Permission denied in /var/www/html/fpc2.php on line 3
but, this file's permission is 777.

Right. So it is a permissions problem. You need to make sure that the "user" that runs the php code has permission to write into the directory. I don't know to which user to give the permission, but it may be "web". Try:
sudo chgrp -R web /var/www/html
then
sudo chmod -R g+w *

Related

CakePHP permission problems

Hello anybody can help with that problem on old cake app:
[Wed Sep 26 18:58:29.454771 2018] [core:crit] [pid 28046:tid 140483665811200] (13)Permission denied: [client 170.231.113.140:34906] AH00529: /home/vtecnet/public_html/admin/app/webroot/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable and that '/home/vtecnet/public_html/admin/app/webroot/' is executable
In fact the error message is giving you all the information:
[…] ensure [.htaccess] is readable and that '/home/vtecnet/public_html/admin/app/webroot/' is executable
I don’t know which version of CakePHP an old cake app is, since you’re not telling us, but looking at file permissions and chmod should point you in the right direction.
WordPress has a nice article on permissions: https://book.cakephp.org/1.3/en/The-Manual/Developing-with-CakePHP/Installation-Preparation.html#permissions
Something like this could help you out, if you have shell access to the server:
# set the ownership to the webserver user (i. e. www-data)
chown -R <insert_webserver_user_here> /home/vtecnet/public_html/admin/
# set permissions recursively
chmod -R 755 /home/vtecnet/public_html/admin/

PHP: move_uploaded_file doesn't work

I just want to move a temporary file to a specified folder. Here is my code.
$uploads_dir = $_SERVER['DOCUMENT_ROOT'].'/upload';
$name = basename($_FILES["csv"]["name"]);
$tmp_name = $_FILES["csv"]["tmp_name"];
chmod("{$uploads_dir}/{$name}", 0755);
if (!move_uploaded_file($tmp_name, "{$uploads_dir}/{$name}")):
array_push($fileErrArr, "fail to move uploaded file");
endif;
This is the error message from the httpd log. It doesn't even mention the "permission" stuff.
[error] [client XXX] PHP Warning: move_uploaded_file(/var/www/html/upload/XXX.csv): failed to open stream: \xe8\xa8\xb1\xe5\x8f\xaf\xe3\x81\x8c\xe3\x81\x82\xe3\x82\x8a\xe3\x81\xbe\xe3\x81\x9b\xe3\x82\x93 in /var/www/html/XXX/action.php on line 115, referer: https://XXX/
[error] [client XXX] PHP Warning: move_uploaded_file(): Unable to move '/tmp/php40tPEO' to '/var/www/html/upload/XXX.csv' in /var/www/html/XXX/action.php on line 115, referer: https://XXX/
Any help is appreciated. Thanks!
I think you can use this <?php echo exec('whoami'); ?> to check your PHP script runner.
Then use ls -l to check the target direction who is the owner.
Make sure the runner and the owner is the same.
Eg: the php runner may be www-data and the target folder owner may be admin. Use:
chown www-data /var/www/xxx/upload
chmod 755 /var/www/xxx/upload
It may work.
Hello Everyone I did encountered this error lately, I am also a beginner and solved this problem in just 3 hours this is the thing that helps me. I use Linux AMI EC2 intance.
The problem might be all about the "Writeability"(sorry I am not really familiar with terms please bear with me) permission of the folder maybe it is only allowed for the root
First make a script on the live serve and include this code to know what is the name
of the client that will execute the move_uploaded_file command of php
<?php
echo exec('whoami');
?>
Then it results to give me "apache" as my client's user
After it look for the folder the destination check if it is writeable by the client's side
using
ls -lh /var/www/folder/
then you will see like
the root and www on it
change it to
chown -R apache:apache /folder destination
Always take note that the user is the result of whoami php script
Hope this helps

Php error (ambigious) [duplicate]

I edited the apache httpd.conf file recently for the mod_rewrite to work. I don't know if this problem originated from that or not, but i'm getting this problem from that day.
This is what I see on the frontend when I run the software -
Server error The website encountered
an error while retrieving
http://localhost/prestashop/. It may
be down for maintenance or configured
incorrectly.
There is no specific file that triggers this. But I saw that usually small programs that consists of 4-5 php files, runs fine, but softwares with a lot of files(like PrestaShop) don't.
I checked the logs and this is what I found.
[Wed Mar 16 19:33:39 2011] [error] [client ::1] PHP Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0
[Wed Mar 16 19:33:39 2011] [error] [client ::1] PHP Fatal error: Unknown: Failed opening required '/var/www/html/yomig/index.php' (include_path='.:/usr/share/pear:/usr/share/php') in Unknown on line 0
[Wed Mar 16 19:33:39 2011] [error] [client ::1] File does not exist: /var/www/html/favicon.ico
[Wed Mar 16 19:33:41 2011] [error] [client ::1] PHP Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0
[Wed Mar 16 19:33:41 2011] [error] [client ::1] PHP Fatal error: Unknown: Failed opening required '/var/www/html/yomig/index.php' (include_path='.:/usr/share/pear:/usr/share/php') in Unknown on line 0
Experienced the same error, for me it was caused because on my Mac I have changed the DocumentRoot to my users Sites directory.
To fix it, I ran the recursive command to ensure that the Apache service has read permissions.
sudo chmod -R 755 ~/Sites
If you are running Fedora, make sure SELinux is not interfering.You fix this with this command:
sudo /sbin/restorecon -R /var/www/
More info here: linuxquestions.org/questions/linux-server-73/
I had the same error and my problem was the file permissions were incorrect.
chmod 755 index.php
worked for me.
It is a SELinux blocking issue, Linux prevented httpd access. Here is the solution:
# restorecon '/var/www/html/wiki/index.php'
# restorecon -R '/var/www/html/wiki/index.php'
# /sbin/restorecon '/var/www/html/wiki/index.php'
Here some guide how to fix it. Go to :
cd /var/www
sudo chown www-data:www-data * -R
sudo usermod -a -G www-data username
Change userneme with your username. I hope it help.
I got this problem when insert wrong file address into .htaccess
php_value auto_prepend_file "/home/user/wrong/address/config.php"
So if you use auto_prepend_file check your file path. It called from .htaccess so PHP can't determine error file and line.
In my mind true way is:
# add READ permission to all directories and files under your DocumentRoot
sudo chmod +r /path/to/DocumentRoot/ -R
# add EXECUTE permission to all DIRECTORIES under your DocumentRoot
find /path/to/DocumentRoot/ -type d -exec chmod +x {} \;
I just came across of this same problem and in my case it was caused by selinux. Disabling it solved the issue. And no, I don't need selinux on my workstation, thank you.
It happened to me today with /home/user/public_html/index.php and the solution was to do chmod o+x /home/user as this directory has to have the X as otherwise the apache server can't list files (i.e. do ls)
This also happens (and is particularly confounding) if you forgot that you created a Windows symlink to a different directory, and that other directory doesn't have appropriate permissions.
Just quote for above answer of user1992554
This one worked perfectly for me in Linux Mint, to get rid of Warning: failed to open stream: Permission denied
cd /var/www
sudo chown www-data:www-data * -R
sudo usermod -a -G www-data username
In Fedora 25, it turned out to be an SE Linux issue, and the notification gave this solution which worked for me.
setsebool -P httpd_read_user_content 1
In my case the group _www that apache uses was missing in the folder's access list, so first I had to add the missing group, like so:
sudo chown -R _www ~/path-to-folder
Change _www to whatever user or group that apache is running as.
Find out apache's user/group using apachectl -S
The output is huge, but look at the very end something like:
User: name="_www"
Group: name="_www"
This isn't a direct answer to the question, but I had the same problem. I installed VSFTPD on my Ubuntu Server VPS. I could upload files, but every file I uploaded didn't have execution permissions (all files had rights "600"). These posts explain explain exactly what you have to do to configure your VSFTPD to set default rights on your files:
Default File Permissions Apache /var/www/
vsFTPd default uploaded file permissions on Ubuntu not working
Once, this happens to me as well. and when I googled the matter, I got to know that this happens when the permissions on the file is wrongfully set to 000 (which means that no one can read, write, or execute that file). Then I just changed my file permission privilege into Read & Write and it's worked for me.
To change file permission settings on mac: Right click on the particular file and click on Get info from dropdown menu and refer to the sharing and permissions panel and change privilege settings into Read & Write
More: http://www.itoctopus.com/warning-unknown-failed-to-open-stream-permission-denied-in-unknown-on-line-0-error-in-joomla
Check dos and unix file format. This problem is seen on linux platforms if dos file format is used. Use doc2unix command like below and then retry it should work
dos2unix *.php
This solution for below problem
Wed Nov 12 07:50:19 2014] [error] [client IP1] PHP Warning: Unknown: failed to
open stream: Permission denied in Unknown on line 0
[Wed Nov 12 07:50:19 2014] [error] [client IP1] PHP Fatal error: Unknown: Failed
opening required '/var/www/html/index.php' (include_path='.:/usr/share/pear:
/usr/share/php') in Unknown on line 0
Go to folder htdocs
cd htdocs
Execute
chmod -R 755 sites
No need to sudo !
Except the permissions, the problem can be open_basedir. If you are using it (and I suggest to use it) then check the settings at the VirtualHost :
php_admin_value open_basedir <YOUR ROOT>

PHP unable to create a directory with mkdir

I have a previously working PHP script that is able to create a directory with mkdir:
$webfolder = "/var/www/html/images/user";
mkdir($webfolder, 0770);
I made some changes to the permission setting of the folder /var/www/html/images which is now:
drwxrwx---. myself apache system_u:object_r:httpd_sys_content_t:s0 images
I think previously this folder was owned by apache. But since apache has the full privileges of read, write and execute as a user group, I wonder why it can't create a folder within. Using the mkdir produces a false boolean value.
Is the problem due to directory ownership or is there some other reasons? Note that I am using PHP version 5.4.
Error Log added:
[Mon Dec 17 11:12:34 2012] [error] [client 127.0.0.1] PHP Warning:
mkdir(): Permission denied in /var/www/html/upload on line 33,
referer: https://mywebsite.com/referer
The answer is staring right in front of me, but I miss it due to my unfamiliarity with SELinux.
The SELinux context type should be set as httpd_sys_content_rw_t instead of httpd_sys_content_t so that the folder is both readable and writable for apache. Changing the context recursively is done with the following command:
# chcon -R -t httpd_sys_content_rw_t /var/www/html/images
Good grief. Hope it helps others who come across this.
On CentOS7 VM, with PHP5.4.16/Apache 2.4.6/mariadb 5.5.44, the smarty template directory was not writable to generate compiled templates files and was giving the below error (in /var/log/httpd/error_log):
[Thu Mar 31 12:36:08.201383 2016] [:error] [pid 13094] [client 192.168.212.65:52204] PHP Fatal error: Smarty error: unable to write to $compile_dir '/var/www/html/app1/templates_c'. Be sure $compile_dir is writable by the web server user. in /var/www/html/app1/libs/smarty/Smarty.class.php on line 1093
hence the PHP application was displaying blank screen.
chmod 777 templates_c did not work either; but as per the suggestion by #Question Overflow, permission on web root on this VM did solve the problem.
I had to execute:
[root#appserver html]# chcon -R -t httpd_sys_content_rw_t /var/www/html
of course, the templates_c & cache should be owned by apache user:
drwxr-xr-x. 2 apache apache 6 Mar 31 12:56 templates_c
drwxr-xr-x. 2 apache apache 6 Mar 31 12:56 cache
After spending more than half a day, came across this.
Thanks

fopen() fails to open stream: permission denied, yet permissions should be valid

So, I have this error:
Warning: fopen(/path/to/test-in.txt) [function.fopen]: failed to open stream: Permission denied
Performing ls -l in the directory where test-in.txt is produces the following output:
-rw-r--r-- 1 $USER $USER 1921 Sep 6 20:09 test-in.txt
-rw-r--r-- 1 $USER $USER 0 Sep 6 20:08 test-out.txt
In order to get past this, I decided to perform the following:
chgrp -R www-data /path/to/php/webroot
And then did:
chmod g+rw /path/to/php/webroot
Yet, I still get this error when I run my php5 script to open the file. Why is this happening? I've tried this using LAMP as well as cherokee through CGI, so it can't be this.
Is there a solution of some sort?
Edit
I'll also add that I'm just developing via localhost right now.
Update - PHP fopen() line
$fullpath = $this->fileRoot . $this->fileInData['fileName'];
$file_ptr = fopen( $fullpath, 'r+' );
I should also mention I'd like to stick with Cherokee if possible. What's this deal about setting file permissions for Apache/Cherokee?
Check if the user that PHP runs under have "X" permission on every directory of the file path.
It will need it to access the file
If your file is: /path/to/test-in.txt
You should have X permission on:
/path
/path/to
and read permission on /path/to/test-in.txt
Another reason of this error may be that the directory of file does not exist.
I just add php code before fopen:
if(!file_exists(dirname($file)))
mkdir(dirname($file));
This help me out.

Categories