1

I am using PHP's mkdir function and I am having some difficulty with the $mode parameters. If I don't specify a parameter, I get UNIX 755 as the default permission settings of the new directory. I would like to set the permission to be UNIX 777, so I did that as you see here:

$mode = '0700';
mkdir($newdir, $mode);

When I do this a folder is created, but I cannot do anything with it. In fact I cannot even delete it! All I can do is rename it via FTP...

I then tried setting $mode = '0600'; This makes a workable folder, but the permissions are set to UNIX 110. How is this possible? Shouldn't it be a UNIX value of 600? Is there some conversion that I am missing out here? Thanks.

codacopia
  • 2,369
  • 9
  • 39
  • 58
  • possible duplicate of [PHP code mkdir('images','0777') creates a folder with 411 permissions! Why?](http://stackoverflow.com/questions/2251283/php-code-mkdirimages-0777-creates-a-folder-with-411-permissions-why) – Ignacio Vazquez-Abrams Jun 20 '11 at 07:01
  • Which user owns the folder when it's created, and which user are you using via FTP? If the FTP and PHP users don't match, then 0700 will deny access (even if you fix the issue that Ignacio caught ;) – Frank Farmer Jun 20 '11 at 07:07

3 Answers3

1

The mode is supposed to be a number, not a string. Try $mode = 0700; instead.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • That worked, but when I set it to $mode = 0777; and run the file, the UNIX equivalent that the folder gets is set to 0755. I still don't understand that part, but as the other answers say it could be a umask issue. – codacopia Jun 20 '11 at 07:20
  • Yes, that would be a umask issue. You can set the permissions to the final value after the fact though. – Ignacio Vazquez-Abrams Jun 20 '11 at 07:22
0

According to php.net manual mkdir function have this description:

bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )

$mode here is an integer (not a string) and it must be started with "0" because it 8-based (not an 10-based).

Update: (from php.net) Note that you probably want to specify the mode as an octal number, which means it should have a leading zero. The mode is also modified by the current umask, which you can change using umask().

Sergey Kuznetsov
  • 8,591
  • 4
  • 25
  • 22
  • 1
    It does not *need* to start with `0`, but omitting it increases the amount of math one must do to read/write the value. – Ignacio Vazquez-Abrams Jun 20 '11 at 07:05
  • I know, but I mean that it does need to start with "0" in case if you want to use Unix-"standard" permissions (if you want that 755 in the code will be the 755 in the filesystem, or we can use 10-based numbers and then: 493(10) == 755(8)). – Sergey Kuznetsov Jun 20 '11 at 07:18
0

If you want to set it to be 0777, try this:

oldumask = umask(0);
mkdir('mydir', 0777); 
umask($oldumask);

Read more on umask as the directory permissions are a combination of the umask and what you specify.

rajasaur
  • 5,340
  • 2
  • 27
  • 22