6

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

Community
  • 1
  • 1
Radi
  • 159
  • 1
  • 3
  • 13
  • I think this is an owner error. PHP owner is deamon so can you please check your server configuration – Hkachhia Aug 21 '15 at 09:20

3 Answers3

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

Vyacheslav A
  • 781
  • 6
  • 10
1

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.

Gaurav Dave
  • 6,838
  • 9
  • 25
  • 39
  • 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. – Radi Aug 21 '15 at 10:11
  • what is the user and group name of your project? Is it www-data? – Gaurav Dave Aug 21 '15 at 10:13
  • 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 errors – Radi Aug 21 '15 at 12:12
  • 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 myuser` – Roman Apr 19 '23 at 15:39
0

use

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

and check once

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41