Php 简明教程

PHP – Create Directory

计算机文件按层次顺序存储在本地存储设备 (称为 drive ) 中,其中一个目录包含一个或多个文件以及子目录。用于创建和管理目录的各自 DOS 命令在操作系统 Windows、Linux 等等中进行定义。

Computer files are stored in the local storage device (called drive) in a hierarchical order, where a directory contains one or more files as well as subdirectories. Respective DOS commands defined in operating systems Windows, Linux etc. are used to create and manage directories.

PHP 提供了目录管理函数来创建目录、更改当前目录和移除某个目录。

PHP provides directory management functions to create a directory, change the current directory and remove a certain directory.

本章讨论在 PHP 中使用以下目录函数的情况 -

This chapter discusses the usage of the following directory functions in PHP −

The mkdir() Function

mkdir() 函数创建一个新目录,它的路径作为函数之一的参数给出

The mkdir() function creates a new directory whose path is given as one of the parameters to the function

mkdir(
   string $directory,
   int $permissions = 0777,
   bool $recursive = false,
   ?resource $context = null
): bool

Parameters

  1. $directory − The first parameter $directory is mandatory. It is a string with either absolute or relative path of the new directory to be created.

  2. $permissions − The second parameter $permissions is an octal number with four octal digits. The first digit is always zero, second specifies permissions for the owner, third for the owner’s user group and fourth for everybody else.

每个数字都是针对每种权限类型的值的和 -

Each digit is the sum of values for each type of permission −

  1. 1 = execute permission

  2. 2 = write permission

  3. 4 = read permission

$permissions 参数的默认值为 0777 ,这意味着创建目录时启用了执行、写入和读取权限。

The default value of $permissions parameters is 0777, which means the directory is created with execute, write and read permissions enabled.

请注意,在 Windows 操作系统上工作时,$permissions 参数会被忽略。

Note that the $permissions parameter is ignored when working on Windows OS.

  1. $recursive − If true, then any parent directories to the directory specified will also be created, with the same permissions.

  2. $context − This optional parameter is the stream resource.

mkdir() 函数返回真或假,表示函数是否已成功执行。

The mkdir() function returns either true or false, indicating if the function has been successfully executed or not.

Examples

以下是 mkdir() 函数的一些示例。

Here are some examples of mkdir() function.

对 mkdir() 的以下调用在当前工作目录中创建子目录。点表示路径是相对的。

The following call to mkdir() creates a subdirectory inside the current working directory. The dot indicates that the path is relative.

$dir = "./mydir/";
mkdir($dir);

我们可以提供包含要创建的目录的绝对路径的字符串参数。

We can give the string parameter that contains the absolute path of the directory to be created.

$dir = "c:/newdir/";
mkdir($dir);

由于 $recursive 参数设置为 true,对 mkdir() 的以下调用包含当前目录内的嵌套目录结构。

The following call to mkdir() contains nested directory structure inside the current directory, as the $recursive parameter is set to true.

$dirs = "./dir1/dir2/dir3/";
mkdir($dirs, 0777, true);

Windows 资源管理器将显示嵌套目录结构,如下所示:

The Windows explorer will show the nested directory structure as follows −

create directory

The chdir() Function

PHP 中的 chdir() 函数对应于 Linux/Windows 中的 chdircd 命令。它会导致根据需要更改当前目录。

The chdir() function in PHP corresponds to the chdir or cd command in Linux/Windows. It causes the current directory to be changed as required.

chdir(string $directory): bool

此函数的字符串参数是要将当前目录更改到的目录的绝对或相对路径。它返回真或假。

The string parameter to this function is either an absolute or relative path of a directory to which the current directory needs to be changed to. It returns true or false.

The getcwd() Function

getcwd() 函数的工作方式类似于 Ubuntu Linux 中的 pwd 命令,并返回当前工作目录的路径。

The getcwd() function works similar to pwd command in Ubuntu Linux, and returns the path to the current working directory.

Example

使用以下代码段,PHP 会在更改当前工作目录之前和之后显示当前工作目录。新当前目录中创建了一些文件。使用 scandir() 函数列出文件。

With the following code snippet, PHP displays the current working directory before and after changing the current working directory. A couple of files are created inside the new current directory. With the scandir() function, the files are listed.

<?php
   echo "current directory: ". getcwd() . PHP_EOL;
   $dir = "./mydir";
   chdir($dir);
   echo "current directory changed to: ". getcwd() .PHP_EOL;

   $fp = fopen("a.txt", "w");
   fwrite($fp, "Hello World");
   fclose($fp);

   copy("a.txt", "b.txt");
   $dir = getcwd();
   foreach(scandir($dir) as $file)
   echo $file . PHP_EOL;
?>

它将生成以下 output

It will produce the following output

current directory: C:\xampp\php
current directory changed to: C:\xampp\php\mydir
.
..
a.txt
b.txt

The rmdir() Function

rmdir() 函数删除了以参数形式给出的路径的某个目录。要删除的目录必须为空。

The rmdir() function removes a certain directory whose path is given as parameter. The directory to be removed must be empty.

$dir = "c:/newdir/";
rmdir($dir) or die("The directory is not present or not empty");