PHP: mkdir - Manual (original) (raw)
(PHP 4, PHP 5, PHP 7, PHP 8)
mkdir — Makes directory
Description
Parameters
directory
The directory path.
Tip
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.
permissions
The permissions are 0777 by default, which means the widest possible access. For more information on permissions, read the details on the chmod() page.
Note:
permissionsis ignored on Windows.
Note that you probably want to specify the permissions as an octal number, which means it should have a leading zero. The permissions is also modified by the current umask, which you can change usingumask().
recursive
If [true](reserved.constants.php#constant.true), then any parent directories to the directory specified will also be created, with the same permissions.
context
Return Values
Returns [true](reserved.constants.php#constant.true) on success or [false](reserved.constants.php#constant.false) on failure.
Note:
If the directory to be created already exists, that is considered an error and
[false](reserved.constants.php#constant.false)will still be returned. Use is_dir() orfile_exists() to check if the directory already exists before trying to create it.
Errors/Exceptions
Emits an [E_WARNING](errorfunc.constants.php#constant.e-warning) level error if the directory already exists.
Emits an [E_WARNING](errorfunc.constants.php#constant.e-warning) level error if the relevant permissions prevent creating the directory.
Examples
Example #1 mkdir() example
<?php mkdir("/path/to/my/dir", 0700); ?>
Example #2 mkdir() using the recursive parameter
<?php // Desired directory structure <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>e</mi><msup><mo>=</mo><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mi mathvariant="normal">.</mi><mi mathvariant="normal">/</mi><mi>d</mi><mi>e</mi><mi>p</mi><mi>t</mi><mi>h</mi><mn>1</mn><mi mathvariant="normal">/</mi><mi>d</mi><mi>e</mi><mi>p</mi><mi>t</mi><mi>h</mi><mn>2</mn><mi mathvariant="normal">/</mi><mi>d</mi><mi>e</mi><mi>p</mi><mi>t</mi><mi>h</mi><mn>3</mn><msup><mi mathvariant="normal">/</mi><mo mathvariant="normal" lspace="0em" rspace="0em">′</mo></msup><mo separator="true">;</mo><mi mathvariant="normal">/</mi><mi mathvariant="normal">/</mi><mi>T</mi><mi>o</mi><mi>c</mi><mi>r</mi><mi>e</mi><mi>a</mi><mi>t</mi><mi>e</mi><mi>t</mi><mi>h</mi><mi>e</mi><mi>n</mi><mi>e</mi><mi>s</mi><mi>t</mi><mi>e</mi><mi>d</mi><mi>s</mi><mi>t</mi><mi>r</mi><mi>u</mi><mi>c</mi><mi>t</mi><mi>u</mi><mi>r</mi><mi>e</mi><mo separator="true">,</mo><mi>t</mi><mi>h</mi><mi>e</mi></mrow><annotation encoding="application/x-tex">structure = './depth1/depth2/depth3/';// To create the nested structure, the </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7519em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">u</span><span class="mord mathnormal">c</span><span class="mord mathnormal">t</span><span class="mord mathnormal">u</span><span class="mord mathnormal">re</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel"><span class="mrel">=</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0019em;vertical-align:-0.25em;"></span><span class="mord">./</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">pt</span><span class="mord mathnormal">h</span><span class="mord">1/</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">pt</span><span class="mord mathnormal">h</span><span class="mord">2/</span><span class="mord mathnormal">d</span><span class="mord mathnormal">e</span><span class="mord mathnormal">pt</span><span class="mord mathnormal">h</span><span class="mord">3</span><span class="mord"><span class="mord">/</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height:0.7519em;"><span style="top:-3.063em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">′</span></span></span></span></span></span></span></span></span><span class="mpunct">;</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">//</span><span class="mord mathnormal" style="margin-right:0.13889em;">T</span><span class="mord mathnormal">ocre</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">es</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">u</span><span class="mord mathnormal">c</span><span class="mord mathnormal">t</span><span class="mord mathnormal">u</span><span class="mord mathnormal">re</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span><span class="mord mathnormal">e</span></span></span></span>recursive parameter // to mkdir() must be specified.if (!mkdir($structure, 0777, true)) { die('Failed to create directories...'); }// ... ?>
See Also
- is_dir() - Tells whether the filename is a directory
- rmdir() - Removes directory
- umask() - Changes the current umask
Found A Problem?
jack dot sleight at gmail dot com ¶
15 years ago
When using the recursive parameter bear in mind that if you're using chmod() after mkdir() to set the mode without it being modified by the value of uchar() you need to call chmod() on all created directories. ie:
<?php
mkdir('/test1/test2', 0777, true);
chmod('/test1/test2', 0777);
?>
May result in "/test1/test2" having a mode of 0777 but "/test1" still having a mode of 0755 from the mkdir() call. You'd need to do:
<?php
mkdir('/test1/test2', 0777, true);
chmod('/test1', 0777);
chmod('/test1/test2', 0777);
?>aulbach at unter dot franken dot de ¶
26 years ago
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);
?>
14 years ago
Remember to use clearstatcache()
... when working with filesystem functions.
Otherwise, as an example, you can get an error creating a folder (using mkdir) just after deleting it (using rmdir).
20 years ago
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!chelidze dot givia at gmail dot com ¶
2 years ago
When creating a file using mkdir() the default root will be the DocumentRoot (in XAMPP) itself.
C:\xampp\htdocs\project/includes/something.php
If you use mkdir("myfile") in something.php, instead of creating the folder in includes, php will create it in the project folder