mkdir not obeying permission parameter
I am using mkdir in a php script to make file3 in directory /file/file2/file3
. I use permission 777 but the file always looks like dr----x--t 2 admin admin 4096 Jul 1 19:26 file3
once it is made. I am running Centos 5 64bit. file2 is permission drwxrwxrwx 16 root root 4096 Jul 1 19:26 file2
already. Anyone 开发者_StackOverflow中文版have any ideas why this is?
mkdir("/file1/file2/file3",777);
You want to use 777
octal, not decimal:
mkdir ("/file1/file2/file3", 0777);
777
decimal turns out to be 1411
octal which will give you the bitmask 1 100 001 001
which is why you're getting those "strange" permissions. The standard set (last three segments) gives you r----x--x
and the first segment modifies the world permissions to t
(the sticky bit).
Also keep in mind that mkdir
is subject to your umask
setting and may not give you the permissions you ask for (your umask
setting gets "removed" from the permissions you ask for to give you the actual permissions). See here for details, including how to avoid the problem.
You're better off using mkdir
to create the directory then chmod
(which isn't affected by your umask
setting) to change the permissions.
mkdir()
is also affected by the current umask()
, See example #1
$old = umask(0);
chmod("/path/some_dir/some_file.txt", 0755);
umask($old);
Also have a look at the note
Note:
Avoid using this function in multithreaded webservers. It is better to change the file permissions with chmod() after creating the file. Using umask() can lead to unexpected behavior of concurrently running scripts and the webserver itself because they all use the same umask.
A little bit background can be found at Wikipedia: umask
I've run into issues with this before and ended up working around it using.
chmod ("/file1/file2/file3",777)
Never solved the actual issue though...
精彩评论