1

I have a script that automatically creates a directory if it doesn't exist and sets permissions to 777. The problem is, it's only setting permissions to 755:

if (!file_exists($this->get_upload_dir()))
    mkdir($this->get_upload_dir(), 0777);

If I check the owner of the directory it created, it shows www-data [33]. I'm not sure what the 33 means. Is www-data the user the server uses to create directories? If so, how can I allow it to create a directory with 777 permissions?

Also note, I have already set the following command:

sudo chown -R www-data:www-data /var/www/html
  • Where are you uploading it to? Is the parent directory one that is controled by another user, such as root? – ilarsona Sep 01 '14 at 18:45

1 Answers1

4

www-data is the user that the apache2 webserver runs as in Ubuntu. (Usually that user has a UID of 33, the number you see.) So files created using the webserver are owned by that file. When you create a file thus, the umask of the server comes into play and any permissions you set are controlled by the umask.

You can work around this in three ways:

  1. Use the chmod PHP function to set the permission after creating.
  2. Use the umask PHP function to set the umask before creating. Be warned, this will set the umask for all threads in a multi-threaded server and can have unintended consequences.
  3. Set the umask for the web server itself. This is worse than (2) in that every process and every site configured in the server is affected.

The best option is to use (1). Have a look at this SO answer for an example of (1), this SO question for a discussion of (2) and this SF question for an example of (3).

muru
  • 197,895
  • 55
  • 485
  • 740
  • As an add-on, do not forget to precede the file mode with a 0. For my case, I tried mkdir('path', 777) and it did not work. Is was because the number is expected to be Octal – Niket Pathak Dec 07 '17 at 15:35