Radi
Radi

Reputation: 179

PHP: "failed to open stream: Permission denied"

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

Upvotes: 7

Views: 24768

Answers (3)

Vyacheslav A
Vyacheslav A

Reputation: 831

  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.

Upvotes: 8

Gaurav Dave
Gaurav Dave

Reputation: 7504

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.

Upvotes: 2

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

use

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

and check once

Upvotes: 0

Related Questions