PHP: "failed to open stream: Permission denied"
Asked Answered
P

3

7

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

Prying answered 21/8, 2015 at 9:2 Comment(1)
I think this is an owner error. PHP owner is deamon so can you please check your server configurationDesegregate
A
2

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.

Ammoniacal answered 21/8, 2015 at 9:15 Comment(4)
When i change the user which .httpd runs under i begin to receive even more error even with a user that is in the sudoers file.Prying
what is the user and group name of your project? Is it www-data?Ammoniacal
no the path to the project is /var/www/php/site .The owner all file and folders in php is my user and the group is apache but even when i 'chown -R apache:apache php' still nothing happens same errorsPrying
After many years of doing chown every time I sync server files with local ones, at last! I modified apache2.conf lines: # These need to be set in /etc/apache2/envvars User ${APACHE_RUN_USER} Group ${APACHE_RUN_GROUP} And now it just works. Exact thing - added myuser to group www-data and modified apache conf with User myuserBricky
M
8
  1. 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');

  2. 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.

Masterson answered 10/8, 2020 at 14:10 Comment(0)
A
2

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.

Ammoniacal answered 21/8, 2015 at 9:15 Comment(4)
When i change the user which .httpd runs under i begin to receive even more error even with a user that is in the sudoers file.Prying
what is the user and group name of your project? Is it www-data?Ammoniacal
no the path to the project is /var/www/php/site .The owner all file and folders in php is my user and the group is apache but even when i 'chown -R apache:apache php' still nothing happens same errorsPrying
After many years of doing chown every time I sync server files with local ones, at last! I modified apache2.conf lines: # These need to be set in /etc/apache2/envvars User ${APACHE_RUN_USER} Group ${APACHE_RUN_GROUP} And now it just works. Exact thing - added myuser to group www-data and modified apache conf with User myuserBricky
S
0

use

$_SERVER["DOCUMENT_ROOT"]."/myFolder/path to upload folder". 

and check once

Streptokinase answered 21/8, 2015 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.