★ wanayoo — archive 1999 http://uk.php.net/manual/pl/function.mkdir.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<lstatmove_uploaded_file>
view the version of this page
Last updated: Thu, 15 Jul 2004

mkdir

(PHP 3, PHP 4 , PHP 5)

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



add a note add a note User Contributed Notes
mkdir
andrei at bizland dot ro
24-Oct-2004 05:33
Here is a better function to create multiple directories :

   function mkDirE($dir,$dirmode=700)
   {
       if (!empty($dir))
       {
           if (!file_exists($dir))
           {
               preg_match_all('/([^\/]*)\/?/i', $dir,$atmp);
               $base="";
               foreach ($atmp[0] as $key=>$val)
               {
                   $base=$base.$val;
                   if(!file_exists($base))
                       if (!mkdir($base,$dirmode))
                       {
                               echo "Error: Cannot create ".$base;
                           return -1;
                       }
               }
           }
           else
               if (!is_dir($dir))
               {
                       echo "Error: ".$dir." exists and is not a directory";
                   return -2;
               }
       }

       return 0;

   }
aidan at php dot net
03-Oct-2004 03:17
This function creates a directory structure recursively.

http://aidan.dotgeek.org/lib/?file=function.mkdirr.php
Han Van den Hoof
09-Nov-2003 06:28
If you're on a shared *nix server, a directory created through mkdir() will not be assigned to you, but to the user that your host's server or php process is running under, usually 'nobody', 'apache' or 'httpd'.

In practice, this means that you can create directories, even add files to them, but you can't delete the directory or its contents nor change permissions.

It is therefore advised to create directories through PHP's FTP API. Here's a function I wrote:

<?php
// create directory through FTP connection
function FtpMkdir($path, $newDir) {
  
      
$server='ftp.yourserver.com'; // ftp server
      
$connection = ftp_connect($server); // connection
  
 
       // login to ftp server
      
$user = "me";
      
$pass = "password";
      
$result = ftp_login($connection, $user, $pass);

  
// check if connection was made
    
if ((!$connection) || (!$result)) {
       return
false;
       exit();
       } else {
        
ftp_chdir($connection, $path); // go to destination dir
      
if(ftp_mkdir($connection,$newDir)) { // create directory
          
return $newDir;
       } else {
           return
false;       
       }
  
ftp_close($conn_id); // close connection
  
}

}
?>

Hope this comes in handy for someone.
timo dot hummel at 4fb dot de
06-Oct-2003 04:20
mkdir will create directories with undesired/unexpected owner/group settings in certain circumstandes when SAFE_MODE is on. See the bug report: http://bugs.php.net/bug.php?id=24604
28-Jun-2003 12:00
You might notice that when you create a new directory using this code:

mkdir($dir, 0777);

The created folder actually has permissions of 0755, instead of the specified
0777. Why is this you ask? Because of umask(): http://www.php.net/umask

The default value of umask, at least on my setup, is 18. Which is 22 octal, or
0022. This means that when you use mkdir() to CHMOD the created folder to 0777,
PHP takes 0777 and substracts the current value of umask, in our case 0022, so
the result is 0755 - which is not what you wanted, probably.

The "fix" for this is simple, include this line:

$old_umask = umask(0);

Right before creating a folder with mkdir() to have the actual value you put be
used as the CHMOD. If you would like to return umask to its original value when
you're done, use this:

umask($old_umask);
aulbach at unter dot franken dot de
22-Jul-1999 10: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:

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

<lstatmove_uploaded_file>
 Last updated: Thu, 15 Jul 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: Kewlio.net Limited
Last updated: Thu Nov 4 22:17:23 2004 GMT