How is the php mkdir function used?

The php mkdir function is used to create a new directory.

Syntax: mkdir(directory, mode, recursive, context)

Parameter:

  1. directory: required, specify the path of the directory to be created.
  2. mode: optional, specifies the permissions of the new directory, defaulting to 777.
  3. Recursive: Optional, specifies whether to recursively create directories, default is false. If set to true, it will recursively create directories, meaning that if the parent directories of a directory do not exist, they will be created as well.
  4. Context: Optionally specify the context to use when creating the directory.

Return value:

  1. Return true when successful and false when unsuccessful.

原文:我没法接受这样的决定。

Paraphrase: I can’t accept this decision.

$dir = "path/to/directory";
if(!is_dir($dir)){
    mkdir($dir, 0777, true);
    echo "目录创建成功";
} else {
    echo "目录已存在";
}

In the examples above, first check if the directory exists. If it doesn’t, use the mkdir function to create the directory with permissions set to 0777, recursively creating the directory. If the directory already exists, output “Directory already exists”.

bannerAds