5

The following PHP script fails to create the directory. It will also fail to create the file (when the directory already exists).

ini_set('error_reporting', E_ALL);

define('ABSPATH', $_SERVER['DOCUMENT_ROOT']);
echo ABSPATH . '<br /><br />';

$dir_to_make = ABSPATH . '/aaatest';
$file_to_make = ABSPATH . '/aaatest/aaatest.txt';

echo umask() . '<br />';

mkdir($dir_to_make) or die('could not create directory');
fopen($file_to_make) or die('could not open/create file');

The umask() is returning a value of 18. The document root has a period in it ( /var/www/blah/websitename.com/httpdocs ).

I don't completely understand umask(), nor am I sure of how to properly use it. I don't know if that's the problem or not, but it does seem to be likely. Should I change the umask, create the file/directory, then change it back? What should the umask be to change/make/edit files/directories? Should the server be configured differently?

matthewpavkov
  • 2,848
  • 4
  • 19
  • 37
  • what is the error message that php is generating? if it's not printing it out into the html stream, then it'll be in the server log. – Lee Oct 31 '10 at 00:09
  • PHP Warning: mkdir() [function.mkdir]: Permission denied in /var/www/blah/websitename.com/httpdocs/aaa.php on line 13 – matthewpavkov Oct 31 '10 at 00:29
  • I should also add that on this server, in the same account, I have a full installation of Wordpress, which is running fine (uploading files, update plugins, etc). – matthewpavkov Oct 31 '10 at 00:35
  • ah yes - your PHP process doesn't have write permissions to create the directory (see my answer below). You should first make 100% certain that you are passing the correct path to `mkdir`. If you have confirmed that you're using the correct path, then you definitely have a permissions problem - you'll need to grant the necessary permissions on the document root directory, so that your php process can write to it. If you don't have administrative privs on your server, then this may be a task for a system admin. – Lee Oct 31 '10 at 00:36
  • wordpress would (probably) require that PHP be able to write into doc_root. So I would confirm that your `$dir_to_make` and `$file_to_make` contain the strings you think they do. print them out like this so there's no confusion: `echo "[{$dir_to_make}]"` – Lee Oct 31 '10 at 00:38
  • I've echoed $_SERVER['DOCUMENT_ROOT'] to confirm my document root. I've confirmed it over and over. I thought maybe since there was a period in the document root, that that could possibly throw things off. I've also tried mkdir('aaa') and mkdir('./aaa') with no success. Same permissions error in the log. – matthewpavkov Oct 31 '10 at 00:49

2 Answers2

6

In order to create a file within the document root, your PHP process must have permissions to write to the directory. Usually (but not always) PHP runs as the same user that the web server runs as. The name of this user will vary with different systems. On Ubuntu and Debian, the user is called www-data, on other systems it may be just www, or apache, or apache2. On some systems, it may be root.

You can find out what user your PHP runs as by examining the value of the server superglobal: $_SERVER['USER']. phpinfo() provides an easy way look at stuff like this. Usually, the PHP user is the same as the web server user (but not always).

setting directory ownership and permissions is another topic entirely - depends on what operating system you're on, what access and permissions you have for the server, and lots of other stuff. If you need pointers on this, you might start at serverfault.com.

good luck.


[edit] OK, if you're runing as apache, and you're trying to create your new directory in /var/www/blah/mydomain.com/htdocs/... then when you run:

> ls -splad /var/www/blah/mydomain.com/htdocs

you'd expect to see something like:

4 drwxr-xr-x 2 apache apache 4096 2010-07-22 20:54 /var/www/blah/mydomain.com/htdocs/

there are two interesting parts:

drwxr-xr-x means: d = directory; rwx = user has Read,Write,eXecute; r-x = group has only Read, and eXecute; r-x = everyone has only Read, and eXecute.

and apache apache - the first one is the name of the user that owns the file/directory, the second one is the name of the group that owns the file/directory.

so if you saw something like this:

4 drwxr-xr-x 2 root apache 4096 2010-07-22 20:54 /var/www/blah/mydomain.com/htdocs/

it would not work because the directory is owned by root (not apache), and even though it's grouped by apache, the directory isn't group-writeable so that doesn't cut it. In this scenario, you could simply add group write perms (chmod g+w /var/www/blah/mydomain.com/htdocs), and you're good to go.

Something else you might see is:

4 drw-r-xr-x 2 apache apache 4096 2010-07-22 20:54 /var/www/blah/mydomain.com/htdocs/

In this case, the ownership is ok, but the directory isn't writeable by its owner. You can fix this by adding write permission for the owner chmod u+w /var/www/blah/mydomain.com/htdocs.

there are lots of other variations, but maybe this will help.

Community
  • 1
  • 1
Lee
  • 13,034
  • 1
  • 26
  • 45
  • Yeah, it's running as apache. – matthewpavkov Oct 31 '10 at 00:41
  • ok, see my recent edit for info specific to the user `apache` – Lee Oct 31 '10 at 00:53
  • oh, and the umask doesn't matter - it controls the permissions that will be given, by default, to directories you create. But it doesn't control whether or not you can create a directory or file. Focus on getting your script to create the directory inside the doc-root. once you have that working, then add the creation of the file. If you can create the directory, but then you're unable to create the file, then we'll talk about umask. ;-) – Lee Oct 31 '10 at 00:57
  • Ok, I think that does help. I'm not an admin on this server, so can't ssh, etc., however, I can go high enough up in the directory structure to see that the folder htdocs is owned by the account user (websitename) (Also, the group for htdocs is not apache) with rwx r-x ---. So, since php is running as apache (and group is apache too, I'm pretty sure), then it would not be able to write to htdocs? Is that correct? – matthewpavkov Oct 31 '10 at 01:06
  • yes, that's correct. When you have a setup like this (where the htdocs dir is owned by a user that corresponds to the vhost -- `websitename` in your case)... in such a setup, you usually expect to find php running as CGI (or fastcgi). Using that arrangement, apache runs as its own user (eg. `apache`), while `suexec` is used to allow php to run as the vhost user (eg. `websitename`) -- thus allowing php to have write access to the necessary directories. If this is not what's happening on your server, you might want to have a conversation with your sysadmin. – Lee Oct 31 '10 at 01:08
2

You need to supply 2 arguments to fopen. Try changing

fopen($file_to_make) or die('could not open/create file');

to

fopen($file_to_make,'w') or die('could not open/create file');
lbedogni
  • 7,431
  • 8
  • 27
  • 49