★ wanayoo — archive 1999 http://uk.php.net/manual/en/function.mkdir.phpNouvelle recherche | Portail wanayoo
PHP
Monday, January 07, 2002  
downloads | documentation | faq | support | reporting bugs | links 

search for in the  


previouslinkinfo
move_uploaded_filenext

Last updated: Sat, 05 Jan 2002
view this page in English | Brazilian Portuguese | Czech | Dutch | French | German | Hungarian | Italian | Japanese | Korean | Polish | Russian | Spanish | Turkish | Plain HTML

mkdir

(PHP 3 <= 3.0.18, PHP 4 >= 4.0.0)

mkdir -- Makes directory

Description

int mkdir (string pathname, int mode)

Attempts to create the directory specified by pathname.

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

mkdir ("/path/to/my/dir", 0700);

Returns TRUE on success and FALSE on failure.

See also rmdir().

User Contributed Notes
mkdir
add a note about notes
aulbach@unter.franken.de
22-Jul-1999 06:37

This is an annotation from Stig Bakken:

The mode on your directory is affected by your current umask.  It will
end
up having (<mkdir-mode> and (not <umask>)).  If you want to
create one
that is publicly readable, do something like this:

  $oldumask = umask(0);
  mkdir('mydir', 0777); // or even 01777 so you get the sticky bit set
  umask($oldumask);

rlynch@ignitionstate.com
11-Feb-2000 11:42

umask affects permissions when files and directories are created.
umask is short for "Un-Mask".
So "not" umask is "and"ed with the permissions of
mkdir, fopen, etc.
In a sense, the umask setting is "subtracted" from the
permissions given to mkdir.
Example:

umask(011);
mkdir('foo', 0777);

will actually make a directory with permissions 0766.

tricord@tricordnet.com
20-Apr-2000 01:45

I don't know how to solve this problem, but I know what causes it. A file
or a dir chmod'ed to 0777 is seen as "unsecure" on some systems,
because it has full read/write/execute permissions for everybody. Therefore
access is blocked to most applications (including PHP3) to prevent a
security hole.
Best thing is to use chmod 0775. If you create files with PHP, you can
reopen them, delete them, write to them and execute them as you wish. 0777
is never a good idea, unless you really need it...

mbsrkch@soback.kornet21.net
01-Aug-2000 08:57

I created several directories with using MKDIR on Sun Solaris. Here is the
source:
<?
$uploadDir1 = "./data/";
$stmt = "select count(*) from CA_ACCEPT";
	$count_stmt=OCIParse($conn,$stmt);
	OCIExecute($count_stmt);
	OCIFetchInto($count_stmt,&$row);
	
	$total_count = $row[0];

	$total_attach ="/";
	$total_count1 = $total_count.$total_attach;
	
	$uploadDir = $uploadDir1.$total_count1;
	echo("$uploadDir");
	
	mkdir("$uploadDir",0777);
?>
Whenever a user attaches a file, The above source will create automatically
directory with the sequence number.

Hope that the source is pretty useful.

gjukema@jukeware.com
22-Jan-2001 11:40

I needed a clean ForceDirectories function as used in Delphi.  I grabbed
the source and converted to PHP.  Just pass in a $path like
"/tmp/dir1/dir2", and all "/tmp/dir1/dir2" will be
created if they don't exist - permissions allowed of coarse :).

  function ForceDirectories( $path) {
    if ( strlen( $path) == 0) {
      return 0;
    }
    //
    if ( strlen( $path) < 3) {
      return 1; // avoid 'xyz:\' problem.
    }
    elseif ( is_dir( $path)) {
      return 1; // avoid 'xyz:\' problem.
    }
    elseif   ( dirname( $path) == $path) {
      return 1; // avoid 'xyz:\' problem.
    }
  
    return ( ForceDirectories( dirname( $path)) and mkdir( $path, 0775));
  }

neale-php@woozle.org
06-Mar-2001 04:53

[Editor's note: This contribution was answering to another post (deleted
because this is not a support forum) asking about the meaning of permission
bits. This post is a good intro to the permissions in Unix/Linux.]

Whoa there, big guy.  File permissions (and umasks) aren't that
complicated.  Octal is your friend.

The three bits in octal correspond to the three flags on the ls -l,
namely:

owner   everyone else
| group |
|   |   |
rwx rwx rwx
7   7   7

The "x" is the low bit, so "--x" would just be 1. 
"w" is the middle bit, "-w-" is 2.  "r" is
the high bit, "r--" is 4.  if you want "r-x", add 1 and
4 for 5.  "rwx" is 7.  Easy.  Just remember to put the 0 in front
(that's why octal is your friend).

Umask is all the bits that you don't want set on file creation.  If your
umask is "022" it means that when you create a new file, it will
never have the "w" bit set in the second or third positions
(group or others).  A more mathematical way of writing this is that your
initial creation bits get anded with the unary negation of the mask (mode
& ~mask).  If you want to force bits on, use a "chmod"
command after you've created the file (or directory, in this case).

If you're still confused, go read <http://www.geek-girl.com/Unixhelp/tasks/permissions_default.html>.
 She says the umask is subtracted to make it easier to understand (it's
actually inverted and anded) but it's a good resource anyway.  And remember
that leading 0!

daniel.wahlgren@nackademin.nacka.se
09-Jul-2001 12:18

If you are using Windows and php, you might know that the permissions 777
is not supported.
You still have to use them in order for mkdir() to work though.

tac@smokescreen.org
13-Jul-2001 09:24

To check if the directory exists before calling mkdir(), use files_exists,
e.g.

if (!file_exists($path)) {
	mkdir($path, 0777);
}
$dir = dir($path);

jiblet@visi.com
15-Aug-2001 07:19

I just wanted to clarify why the umask isn't technically being subtracted. 
Suppose you want to make sure that no file gets created with write
permission for everyone. You would use the umask:

0002 (-------w-)

If you then created a directory with the following permissions:

0774 (rwxrwx--x)

subtraction would yield:

0772 (rwxrwx-w-)

which is exactly what you DIDN'T want.  That's why instead, the umask is
inverted then "BITWISE AND"ed to negate the umask's bit(s) and
preserve any existing bits:

775 (rwxrwxr-x) & (bitwise AND)
774 (rwxrwx--x) =
774 (rwxrwx--x)

KilerCris@Mail.com
28-Aug-2001 07:12

I noticed that this function will only attempt to create the last directory
in the path, so 
mkdir("./newdir1/newdir2/newdir2",0700);

won't work. I wrote this function to do it.

function mkpath($path,$mode = 0700) {
  $dirs = explode("\\",realpath($path));
  $path = $dirs[0];
  for($i = 1;$i < count($dirs);$i++) {
    $path .= "/".$dirs[$i];
    if(!is_dir($path))
      mkdir($path,$mode);
  }
}

Note: I wrote this on windows, where realpath starts with x:\ (x being
drive letter)

matt-nospam@sprynewmedia.com
12-Oct-2001 01:42

Thought this might be handy for those running on a *nix type system.

function mkdirs($mode, $path)
{
  exec("mkdir -m $mode -p path");
}

Note: Haven't tested this. But I'm sure some variation will work nicely.

For more info check out 
"man mkdir" on your *nix box.

add a note about notes


previouslinkinfo
move_uploaded_filenext

Last updated: Sat, 05 Jan 2002

show source | credits | mirror sites:  

Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: On-Line PLC
Last updated: Mon Jan 7 11:51:31 2002 GMT