mkdir

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

mkdirCria um diretório

Descrição

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

Tenta criar o diretório especificado pelo caminho pathname.

Parâmetros

pathname

O caminho do diretório.

mode

O modo padrão é 0777, que significa o acesso mais abrangente possível. Para mais informações sobre os modos, leia os detalhes na página da chmod().

Nota:

O parâmetro mode é ignorado no Windows.

Note que você provavelmente quer especificar o mode como um número octal, o que significa que ele deve ter o zero inicial. O mode é também modificado pela umaks atual, que você pode mudar usando umask().

recursive

O padrão é false.

context

Um resource de contexto de stream.

Valor Retornado

Retorna true em caso de sucesso ou false em caso de falha.

Changelog

Versão Descrição
5.0.0 O parâmetro recursive foi adicionado
5.0.0 A partir do PHP 5.0.0, mkdir() também pode ser usada com alguns wrappers de URL. Veja em Protocolos e Wrappers suportados uma lista de quais wrappers suportam mkdir()

Exemplos

Exemplo #1 Exemplo de mkdir()

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

Notas

Veja Também

add a note

User Contributed Notes 4 notes

up
29
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
13
aulbach at unter dot franken dot de
23 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
4
Protik Mukherjee
18 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
2
julius - grantzau - c-o-m
12 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