PHP: "failed to open stream: Permission denied" - php

I am getting some interesting results on my server when i try to access any Directory or File via some Function.I have set all my file & directory permissions to 777 and have changed the content owner to Apache but i still get error messages.Code:
move_uploaded_file($_FILES['file']['tmp_name'], '/var/www/html/fileContent_Site/userData/'.$_SESSION['username'].DIRECTORY_SEPARATOR.$_FILES['file']['name']);
Or
file_put_contents('userData/userData.txt', $result,FILE_APPEND);
mkdir("userData/".$register['username']);
For 'move_uploaded_file()' i get:
move_uploaded_file(/var/www/php/Site/userData/radi/110729.png):failed to open stream: Permission denied in /var/www/php/Site/upload.php
move_uploaded_file(): Unable to move '/tmp/phpUFvMcn' to '/var/www/php/Site/userData/radi/110729.png' in /var/www/php/Site/upload.php
And for 'file_put_content()' and 'mkdir()'
file_put_contents(userData/userData.txt): failed to open stream: Permission denied in /var/www/php/Site/register.php
mkdir(): Permission denied in /var/www/php/Site/register.php

Check owners that runs PHP. To check - simply add these strings near your "file_put_contents" in your PHP file
echo "current user: ".get_current_user();
echo "script was executed under user: ".exec('whoami');
If you see the difference between current user and "script user", then you've found the issue.
Output example:
current user: root
script was executed under user: www-data
Just set the appropriate user to your PHP files directory/directory you want to write from your PHP script:
In Linux terminal execute:
chown -R www-data:www-data /path/to/the/folder
please, note, that "www-data" user is only for example. You should use your user you get from the "script was executed under user" output.
P.S:
To check folder owner, you could use this linux command:
ls -ltr
P.P.S:
check if your folder has the right access permission: 755
The folder php files should have "644" access permission.
To check permission, use the same command as for the owner check:
ls -ltr
You'll see something like:
drwxr-xr-x 10 www-data www-data 4096 Aug 5 15:18 api
Where "drwxr-xr-x" is access permission. Google it, to get more info about.

Open http.conf (in /opt/lampp/etc/httpd.conf) file.
Edit this part:
<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
#
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User hostname
Group hostname
</IfModule>
See, if that works.

use
$_SERVER["DOCUMENT_ROOT"]."/myFolder/path to upload folder".
and check once

Related

Permission denied with user ubuntu but not with user www-data on .csv file

I'm seeing weird Ubuntu permission issues only on a single .csv file when trying to access with php. The file is list.csv, owned by ubuntu:www-data, and ubuntu user is part of www-data group. The rest of the site works fine, no permission issues, but I see the following error when trying to load this file with php:
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(xxx/list.csv): failed to open stream: Permission denied'
If i do chown www-data:www-data list.csv, i can load with php. Why is this file not being loaded by php if owned by ubuntu, even if ubuntu is part of www-data group?
Additionally, I can open this file as ubuntu user (vi list.csv) read and write.
Edit: Permissions on the file are: -rwxr-xr-x 1 ubuntu www-data
Edit again: Changed permissions to 0644 -rw-r--r-- 1 ubuntu www-data no luck
Edit some more: File lives in a directory that has x permission:
drwxr-xr-x 3 ubuntu www-data 4096 Jul 28 23:09 content/
Not sure it has anything to do with execute permissions as I can change owner to www-data:www-data and the file gets loaded. It would seem its something to do with user ubuntu owning the file and not www-data
Edit: Its this line of code that errors out, trying to load .csv file into php SplFileObject:
SplFileObject::__construct("xxx/content/list.csv");
I can see in the OP that the only difference between the owner and group is write permission (you have tried xr and r with the group). So, I suspect your PHP is trying to open the file in write mode.
To confirm this try:
$myCSV = new SplFileInfo('YOUR CSV');
$myCSV->isReadable(); // should be true
$myCSV->isWritable(); //expecting this to be false
Edit: If that's whats going with your program you can open the file just in read mode using: $myCSV->openFile('r')
Your server might have the PHP open_basedir parameter set which would prevent access to any files outside of this directory.
To verify this have a look into your relevant php.ini file (for example /etc/php5/apache2/php.ini) and check if the open_basedir line is set.

Asterisk Failed to execute '/usr/share/asterisk/agi-bin/call_handle.php': Permission denied

Server: Ubunto
Asterisk Version: 11.x
Directory: /usr/share/asterisk/agi-bin/
File: call_handle.php
Directory Permission: 755 root root
File Permission: 755 root root
asterisk.conf
astagidir => /usr/share/asterisk/agi-bin
extensions.conf
same => n,AGI(call_handle.php)
When I dial any number this AGI PHP Script is run.
But, It will give me this message in asterisk console.
Asterisk Failed to execute '/usr/share/asterisk/agi-bin/call_handle.php': Permission denied
I am not sure why this message is comes up. It has execute permission with ROOT user
The probable issue seems to be with the file ('/usr/share/asterisk/agi-bin/call_handle.php') not falling in the user group of Asterisk daemon that is asterisk:asterisk
That is to say - Asterisk daemon should have the ownership of this file.
Secondly, there is also a possibility that the user who is actually running this Asterisk daemon is not having the execute permission of this daemon.It should be there.
I got answer myself:
Set PHP Envirement in my php script and it will run by asterisk.
I have set following in my php file at first line:
#!/usr/bin/env php
Reff: PHPAGI: Exec format error
First ensure file can be executed
chmod a+x /usr/share/asterisk/agi-bin/call_handle.php
After that check that selinux not enabled or setup correctly
Disable selinux on Centos/Redhat
echo "SELINUX=disabled" > /etc/sysconfig/selinux
After that need reboot

How to access a 0750 folder via php script of root?

I have a php script and it owner is ROOT then I want to use it access to other account files and directories but when I try to access a 0750 file or directory I will got an error message say " failed to open dir: Permission denied"!
How can I do? Why the ROOT has no permission?
Root user definitely has the permission to execute everything, but the problem is, apache doesn't run from root.
To solve this, try change owner of the file to the user who runs apache. Commonly it is apache user, but with apache running, you can get it with this command:
ps aux | egrep '(apache|httpd)'
After that, change the owner of the file (assuming apache user is apache):
chown apache:apache file.php
Because this does not depend on the owner of the script, but which account executed the script.

PHP glob/scandir and Linux softlinks/permissions

I'm trying to write a simple online music player to improve my programming.
I have a server running CentOS 6.5, with an external hard disk containing all my files. I mounted my storage partition to /mnt/storage and my music is in /mnt/storage/cascornelissen/Muziek, which has the following permissions:
drwxrwxr-x 19 cascornelissen ftpusers 4096 Nov 4 14:43 Muziek
Then I have a simple httpd webserver, hosting the project in /var/www/html/melodi, which constains a softlink to the Muziek directory mentioned earlier:
lrwxrwxrwx 1 root ftpusers 34 Jan 26 22:31 music -> /mnt/storage/cascornelissen/Muziek
I want to use PHP's glob to list all the files but that returned an empty array. So I tried scandir("music") but it returns the following error:
Warning: scandir(music): failed to open dir: Permission denied in /var/www/html/melodi/index.php on line 42
Any ideas on how to fix this permissions issue?
Try this:
chmod ugo+x /mnt /mnt/storage /mnt/storage/cascornelissen
Probably some part of your path will not allow entry for your Apache user. Allowing entry to a directory means adding x or execute permissions to the directory.
Explanation
# create path a/b
mkdir -p a/b
ls a/b # this works
# now remove all permissions for a
chmod 000 a
ls a/b # ls: cannot access a/b: Permission denied
# but why, we should still have permission for b??
# Now, lets add x permission for a:
chmod ugo+x a
ls a/b # works again
ls a # ls: cannot open directory a: Permission denied
# So, we cannot list contents of a, but we can
# access a/b *through* a
So, execute permission or x for a directory means we can access the path below it, even if we cannot read the contents of the directory itself. In your case, Apache needs permission to "walk through" /mnt/storage/cascornelissen even though it will not read any files until it reaches Muziek.

php writing to file permissions issue

When php-script is trying to write to a file:
-rwxr-xr-x. 1 eugene_val eugene_val 8033 Sep 10 10:47 ajax_EN.json
I get an error:
fopen(ajax_EN.json): failed to open stream: Permission denied
I wonder what could be an appropriate solution to it taking security into consideration.
The options I could think of are:
1) chown this file to apache user and chmod it to 700
2) add apache to a group of the file-owner
3) use suPHP and likes(which I would not like to because of the performance hit)
A better choice is to change the file's group to the Apache user group, and set the file to be group-writable:
$ chgrp <apache_group> ajax_EN.json
$ chmod g+w ajax_EN.json
The permissions of the path for this file also matter. Even if you chmod 777 the file, if the user trying to read it doesn't have read permissions for the path, they still won't be able to read the file.

Categories