Search code examples
phpapachefile-uploadmkdir

PHP - Apache can't write in a directory that just created


I have a scripts that allows to assign files to products. To avoid problems with name files repeating, each product has a specific directory.

The uplad directoy that contains all product-specific directories looks like this.

4 drwxrwx--x 3 pepe pepe 4096 Jan 9 17:08 .
20 drwxrwxr-x 35 pepe pepe 20480 Jan 9 15:42 ..
4 drwxrwxr-x 2 apache apache 4096 Jan 9 17:08 001

Apache runs under user "apache" and "apache" is set as part of group "pepe".

If I try to upload a file to a product that doesn't have it's directory created the scripts create the directory (with the permissions shown for dir "001") and then tries to upload the file, but fails.

If I change the directory owner to pepe.pepe in SSH and then reupload the file it goes perfectly.

Why is apache creating a directory that in the next line of code can't write in?

How do I prevent to have to manually change ownership to the directory created, because my client obviously can't do it when assigning the files to all his products.


Solution

  • Can you try chgrp() in your PHP to change the group to "pepe"? If it does not work, at least the errors would give you a hint. Another one comes from "safe mode":

    When safe mode is enabled, PHP checks whether the files or directories being operated upon have the same UID (owner) as the script that is being executed.

    I would try changing (manually) the owning group of the parent dir(s) too to "apache".