search for in the  
<lstatmove_uploaded_file>
Last updated: Thu, 19 May 2005

mkdir

(PHP 3, PHP 4, PHP 5)

mkdir -- Makes directory

Description

bool mkdir ( string pathname [, int mode [, bool recursive [, resource context]]] )

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

Note: Mode is ignored on Windows, and became optional in PHP 4.2.0.

The mode is 0777 by default, which means the widest possible access. For more information on modes, read the details on the chmod() page.

Example 1. mkdir() example

<?php
mkdir
("/path/to/my/dir", 0700);
?>

Returns TRUE on success or FALSE on failure.

Note: As of PHP 5.0.0 mkdir() can also be used with some URL wrappers. Refer to Appendix L for a listing of which wrappers support mkdir().

Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Reference CXXII, Stream Functions.

Note: The recursive parameter was added as of PHP 5.0.0.

Note: When safe mode is enabled, PHP checks whether the directory in which you are about to operate has the same UID (owner) as the script that is being executed.

See also rmdir().



User Contributed Notes
mkdir
tommiboy
18-May-2005 05:09
nilsandre at gmx dot de:

This is what the new "recursive" parameter does in PHP5.

Prior to PHP5, you are right, however do not fall for the idea of writing your own recursive directory builder as I did... not unless you *have* to.
Note that `mkdir -p $dir` will just do fine, is some 5-10 times faster than a recursive "directory builder" in php and more elegant, too. A simple "man mkdir" in time would have saved me hours...
nilsandre at gmx dot de
12-May-2005 08:06
Possibly you should state that if you create a directory, the parent directory must exisit. E.g. if you want to create /website/testsite/, /website must be already existant (add this note to the php manual).
webmaster2007 at home dot nl
28-Apr-2005 10:27
Maybe you can use this:

<?php
  
function open_dir($dir, $newdir){    //The function that will copy the files
      
if(file_exists($dir) && file_exists($newdir)){
          
$open_dir=opendir($dir);
           while (
false !== ($file = readdir($open_dir))) {
               if(
$file != "." && $file != ".."){
                   if(
file_exists($newdir."/".$file) && filetype($newdir."/".$file."/") != "dir"){
                      
unlink($newdir."/".$file);
                   }
                   if(
filetype($dir."/".$file."/") == "dir"){
                       if(!
file_exists($newdir."/".$file."/")){
                          
mkdir($newdir."/".$file."/");
                          
open_dir($dir."/".$file."/", $newdir."/".$file."/");
                       }
                   }
                   else {
                      
copy($dir."/".$file."/", $newdir."/".$file);
                   }
               }
           }
       }
   }
  
  
  
open_dir("Your source map", "Your destination map"); //Here you can fill in your source en destination map
?>
yknot7 at hotmail dot com
13-Apr-2005 01:59
Add permission using CHMOD in the above mentioned

// create directory through FTP connection
function FtpMkdir($path, $newDir) {
 
       $server='ftp.server.com'; // ftp server
       $connection = ftp_connect($server); // connection
 
 
       // login to ftp server
       $user = "me";
       $pass = "pass";
       $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
       ftp_site($connection, "CHMOD 777 $path/$newDir") or die("FTP SITE CMD failed.");
           return $newDir;
       } else {
           return false;     
       }
  
   ftp_close($connection); // close connection
   }

}
FtpMkdir("path","dir");
mdap at dap dot ro
05-Apr-2005 04:26
at this example (from mkdir) :
<?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
  
}

}
?>
i found a mistake at the last line before } :
 ftp_close($conn_id); // close connection
is not $conn_id is $connection
Protik Mukherjee
03-Mar-2005 11:29
mkdir, file rw, permission related notes for Fedora 3////
If you are using Fedora 3 and are facing permission problems, better check if SElinux is enabled on ur system. It add an additional layer of security and as a result PHP cant write to the folder eventhough it has 777 permissions. It took me almost a week to deal with this!

If you are not sure google for SElinux or 'disabling SELinux' and it may be the cure! Best of luck!
Aleks Peshkov
26-Feb-2005 07:18
function makeDirs($strPath, $mode = 0777) //creates directory tree recursively
{
   return is_dir($strPath) or ( makeDirs(dirname($strPath), $mode) and mkdir($strPath, $mode) );
}

The simpler is the better.
jay dot kamminga at home dot nl
24-Feb-2005 12:38
I was looking for a function called mkfile, since it doesn't exist:

<?
function mkfile($filename, $mode = 0777, $contents = null) {
  
$dir = preg_replace("/^(.*\/)[^\/]+$/", "\\1", $filename);
   if (!
is_dir($dir)) { trigger_error("mkfile() failed, no such directory <strong>{$dir}</strong>, ", E_USER_WARNING); return(false); }
   if (
file_exists($filename)) { trigger_error("mkfile() failed (File exists)", E_USER_WARNING); return(false); }
   if (
is_array($contents)) $contents = implode("\n", $contents);
   if (
$fp = fopen($filename, "w")) {
       if (
is_null($contents)) {
           return(
$fp);
       } else {
          
fwrite($fp, $contents);
          
chmod($filename, $mode);
          
fclose($fp);
           return(
true);
       }
   } else {
trigger_error("mkfile() failed (Wrong file permissions)", E_USER_WARNING); return(false); }
}
// mkfile.
?>
par dot sandgren at gmail dot com
11-Feb-2005 02:29
You might want to upload files into random named dir's, just to make it harder to guess the link. But still have some ID to know whats in there.

<?php

$dirLength
= rand(30, 40);
      
$projectID = $_POST['pid']; // ID from database perhaps?
      
      
$rndName = "";
       for(
$i=0; $i<$dirLength; $i++)
       {
          
$rnd = rand(1, 3);
           if(
$rnd == 1)
              
$rndName = $rndName . chr(rand(97, 122));
           else if(
$rnd == 2)
              
$rndName = $rndName . chr(rand(65, 90));
           else
              
$rndName = $rndName . chr(rand(48, 57));
       }
      
      
$dirName = $projectID . "-" . $rndName;
      
mkdir($dirname);
?>
ramonklown at pop dot com dot br
16-Nov-2004 11:59
Right after you make a dir you should put chdir in order to be able to read that dir, otherwise you get an error message.

$dirupload = 'D:/server/images/';

mkdir ($dirupload, 0700);

chdir('./');

Hope it helps someone.

Peace,
Ramon
andrei at bizland dot ro
24-Oct-2004 11: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 09:17
This function creates a directory structure recursively.

http://aidan.dotgeek.org/lib/?file=function.mkdirr.php
Han Van den Hoof
09-Nov-2003 12: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 10: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 06: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 04: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, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT