Jump to content

Recommended Posts

I am sure it is a silly mistake, but don't see it.  Can anyone tell me why I cannot create a directory?  Thanks

echo('filepath1: '.$filepath1.'<br>');
echo('filepath2: '.$filepath2.'<br>');
echo('fileowner: '.posix_getpwuid(fileowner($filepath1))['name'].'<br>');
echo('fileperms: '.substr(sprintf('%o', fileperms($filepath1)), -4).'<br>');
echo('current_user: '.get_current_user().'<br>');
echo('filepath1 exists: '.(file_exists($filepath1)?'Yes':'No').'<br>');
echo('filepath1 is a directory: '.(is_dir($filepath1)?'Yes':'No').'<br>');
echo('filepath2 exists: '.(file_exists($filepath2)?'Yes':'No').'<br>');
if(!file_exists($filepath2)){mkdir($filepath2, 0755);}
filepath1: /var/www/main/user_resources/documents/30
filepath2: /var/www/main/user_resources/documents/30/01
fileowner: NotionCommotion
fileperms: 0755
current_user: NotionCommotion
filepath1 exists: Yes
filepath1 is a directory: Yes
filepath2 exists: No
An error occurred in script '/var/www/main/
application/classes/application.php' on line 197: mkdir(): Permission denied (error no: 2). [12.34.56.789]

 

Link to comment
https://forums.phpfreaks.com/topic/299311-permission-denied-to-make-directory/
Share on other sites

The answer is right in the error message: Permission denied

 

You need to set the correct permissions on the directory you are trying to make directory's in for the user trying to make the directory.

Edited by benanamen

From the manual - mkdir

http://php.net/manual/en/function.mkdir.php

 

 

The mode is 0777 by default, which means the widest possible access. For more information on modes, read the details on the chmod() page.

 

Most likely Php is running as user nobody, not who you think it is.

Edited by benanamen

get_current_user() is not returning the user PHP is running as. Its returning the owner of the script, this is not the user PHP is running as.

 

 

To see who PHP is running as you maybe able to use

echo `whoami`; // yes those are backticks not quotes

@NotionCommotion: Your code checks the permissions of the script owner, but you need to know the UID of the process:

var_dump( posix_getuid() );

That most definitely isn't “NotionCommotion”, rather something like “www-data”.

 

If you want PHP to create directories, you need to make www-data (or whatever the user is called) the owner of the parent directory.

Edited by Jacques1

Well, I feel silly.  PHP is being ran as apache.  Probably better to use something like www-data so it is simpler to change websevers if desired.

 

Still odd that I earlier wasn't able to create the directory.  I changed permissions to 0775, and apache belonged to the NotionCommotion group.  Oh well, all is good now, and probably best not to use groups like I did for this.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.