NotionCommotion Posted November 6, 2015 Share Posted November 6, 2015 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] Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 6, 2015 Share Posted November 6, 2015 (edited) 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 November 6, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted November 6, 2015 Author Share Posted November 6, 2015 Yes, I recognize that permission is denied. Why? As I showed on the original post, the parent directory is 0755 and is owned by the PHP user. Quote Link to comment Share on other sites More sharing options...
benanamen Posted November 6, 2015 Share Posted November 6, 2015 (edited) 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 November 6, 2015 by benanamen Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 6, 2015 Share Posted November 6, 2015 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 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted November 6, 2015 Share Posted November 6, 2015 (edited) @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 November 6, 2015 by Jacques1 Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted November 6, 2015 Author Share Posted November 6, 2015 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.