-3

I keep getting permission denied when using mkdir() in my PHP script.

mkdir: cannot create directory : Permission denied:

$mount = shell_exec( "mkdir -p {$mntdir}/{$hostname} 2>&1");

I try to write to folder

/home/user/mnt

When I type below in the command line it does work, so which permissions am I missing?

php -r "shell_exec('mkdir -p /home/user/mnt/test');"

RHEL7/ PHP Version 5.4.16

Any help would be much appreciated!

marlonlaan
  • 3
  • 1
  • 2

3 Answers3

1

Why not use php function mkdir?

if (!mkdir($dir, 0755, true)) {
    die('Failed to create folders...');
}

If you have warnings enabled, you will receive warnings if directory already exists or you don't have permissions for it.

cpatricio
  • 111
  • 2
0

Is selinux in effect?

Check with command

sestatus 

If it is enforcing - then try setting this parameter

setsebool -P httpd_enable_homedirs true

You can also monitor selinux deny messages via

sealert -a /var/log/audit/audit.log
Dmitry Zayats
  • 1,378
  • 7
  • 7
0

Without seeing the configuration, I assume the problem is the user which runs the PHP script.

When you run the script from shell via command-line PHP, you are running the script as the current logged-in user, which has rights to write to the /home/user/mnt.

However, your web server PHP process is running as another user, which does not have write rights, and therefore the creation fails.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63