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
1
  • Where are you uploading it to? Is the parent directory one that is controled by another user, such as root?
    – ilarsona
    Sep 1, 2014 at 18:45

1 Answer 1

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).

1
  • 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 Dec 7, 2017 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.