0

In my code

$path = 'images/product/'.$pid;
if( ! file_exists($path)) {
    mkdir($path, 0777);
}
...
...

but when I type

ls -l

in terminal

drwxr-xr-x 2 www-data www-data 4.0K 2011-12-10 19:28 1/

This is the permission that I get, but is not I want.

I want to allow web user to upload the project based on the product ID (directory) that created during runtime.

How can I do so?

Js Lim
  • 3,625
  • 6
  • 42
  • 80
  • 3
    Do you really need 777? *Really*? It is far better to apply ownership by users and groups to allow other users/groups on the box to write to that directory. If you're using 777 so Apache can write to the directory then there's no need, it is already owned by `www-data` and anyone uploading a file via PHP/Apache will have write permissions already. – Tak Dec 10 '11 at 13:41
  • I think is not necessary need 777. Thanks for advise. – Js Lim Dec 25 '11 at 05:28

2 Answers2

4

For creating directories with 777 permissions,or any permissions

$path = 'images/product/'.$pid;
if( ! file_exists($path)) {
    $mask=umask(0);
    mkdir($path, 0777);
    umask($mask);
}

...

rjv
  • 6,058
  • 5
  • 27
  • 49
1

This is because by default the Apache umask is set to 0022 by default.

Since umask is to revoke permission. Example

default 0777 rwx.rwx.rwx 
umask   0022 ---.-w-.-w-
Final   0755 rwx.r-x.r-x

There are 2 ways to solve this,

1. Edit /etc/apache2/envvars
   add in **umask** *<permission to be revoke>*
   restart apache
2. add in umask(0000); before the mkdir('mydir', 0777);
Js Lim
  • 3,625
  • 6
  • 42
  • 80