PHP 8.3.4 Released!

mkdir

(PHP 4, PHP 5, PHP 7, PHP 8)

mkdirCrea un directorio

Descripción

mkdir(
    string $pathname,
    int $mode = 0777,
    bool $recursive = false,
    resource $context = ?
): bool

Intenta crear el directorio especificado por pathname.

Parámetros

pathname

La ruta del directorio.

mode

El modo predeterminado es 0777, lo que significa el acceso más amplio posible. Para más información sobre los modos, lea los detalles en la página de chmod().

Nota:

mode es ignorado en Windows.

Observe que probablemente se quiera especificar el modo como un número octal, lo que significa que debería de haber un cero inicial. El modo es modificado también por la actual máscara de usuario, la cual se puede cambiar usando umask().

recursive

Permite la creación de directorios anidados especificado en el parámetro pathname.

context

Nota: Soporte para context fue añadido en PHP 5.0.0. Para una descripción de contexts, refiérase a Flujos.

Valores devueltos

Devuelve true en caso de éxito o false en caso de error.

Ejemplos

Ejemplo #1 Ejmplo de mkdir()

<?php
mkdir
("/ruta/a/mi/directorio", 0700);
?>

Ejemplo #2 mkdir() usando el parámetro recursive

<?php
// Estructura de la carpeta deseada
$estructura = './nivel1/nivel2/nivel3/';

// Para crear una estructura anidada se debe especificar
// el parámetro $recursive en mkdir().

if(!mkdir($estructura, 0777, true)) {
die(
'Fallo al crear las carpetas...');
}

// ...
?>

Errores/Excepciones

Emite un error de nivel E_WARNING si el directorio ya existe.

Emite un error de nivel E_WARNING si los permisos relevantes impiden crear el directorio.

Notas

Ver también

  • is_dir() - Indica si el nombre de archivo es un directorio
  • rmdir() - Elimina un directorio

add a note

User Contributed Notes 5 notes

up
37
jack dot sleight at gmail dot com
13 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);
?>
up
3
chelidze dot givia at gmail dot com
8 months 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
up
19
aulbach at unter dot franken dot de
24 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);
?>
up
6
Protik Mukherjee
19 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!
up
3
julius - grantzau - c-o-m
13 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).
To Top