PHP 目录函数顾名思义是一组用于检索详细信息、修改它们以及获取有关各种文件系统目录及其特定内容的信息的函数。可以对目录执行很多操作,例如创建、删除、更改当前工作目录、列出目录中存在的文件等。这些函数不需要单独安装,因为它们是 PHP 核心的一部分。但要启用 chroot() 功能,我们需要配置 –enable-chroot-func 选项。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
让我们了解一些基本的 PHP 目录功能,如下所示:
我们使用 mkdir() 函数在 PHP 编程脚本中创建一个新目录。
语法:
mkdir($dir_path,$mode,$recursive_flag,$context);
哪里,
示例:
<?php mkdir("/articles/"); echo("Directory created"); ?>
输出:
这是一个基本示例,展示了在我们需要的路径中创建目录。确保路径有足够的权限,否则将抛出“权限被拒绝”错误。
我们分别使用 opendir() 和 readdir() 来打开目录链接并读取它。第 1 步是打开目录,第 2 步是读取它。
第 1 步: 要打开目录链接,opendir() 是我们用来执行此步骤的函数。它需要两个输入参数,如下所示。
语法:
opendir($dir_path,$context);
这将返回资源数据值作为其输出。它提供的资源 ID 将在我们的进一步处理步骤中使用,否则我们会收到错误,因为资源 ID 无效。
第2步:要读取目录的内容,readdir()就是用于此目的的函数,需要递归调用它直到目录到达目录末尾手柄。
示例:
<?php $direct = "/files/"; if (is_dir($direct)){ if ($td = opendir($direct)){ while (($file = readdir($td)) !== false){ echo "filename:" . $file . "<br>"; } closedir($td); } } ?>
输出:
首先在这个例子中,我们声明需要读取的目录路径。我们正在 if 语句中检查该目录是否存在,然后继续打开该目录的内容并读取。输出显示目录中存在的文件名。
我们使用 Closedir() 函数来在读取目录内容后关闭目录。
语法:
$dir_handle = opendir($dir_path); ... ... closedir($dir_handle);
示例:
<?php $dir = "/file1"; if (is_dir($dir)) { if ($dh = opendir($dir)) { $direc = readdir($dh); echo("File present inside directory are:" .direc); closedir($dh); echo("Closed directory"); } } ?>
输出:
在此示例中,我们首先声明目录的路径。然后使用 if 条件语句检查路径是否有效,如果有效,则打开目录,读取其变量,然后关闭它。因此,在目录的打开和关闭之间可以进行任何操作。
我们使用函数 chdir() 来更改它指向的当前工作目录。
语法:
chdir(directory)
它只需要一个参数,即当前工作目录应该指向的目录。成功时返回 true,如果更改目录失败则返回 false。
示例:
<?php // Get current directory echo getcwd()."\n"; // Change directory chdir("/workspace/test"); // Get current directory echo getcwd(); ?>
输出:
In this example, we are first printing the present working directory. Then we are changing the same using chdir function to “test” directory and printing the same on the output. Hence make sure the entire path we are giving here exists.
We use the function chroot() for changing the root directory of the ongoing process to the directory path we pass as an argument in this function. Also, the present working directory path will be changed to “/”. To perform this function one needs root permission/privileges.
Syntax:
chroot(directory)
Example:
<?php // Changing root directory path chroot("/change/path/dir/"); // Displaying present directory echo getcwd(); ?>
Output:
In this example, we are first using the chroot function to change the path of the root directory. Next, we are displaying the present working directory which will be now changed to home path.
For this purpose, we are using rewinddir() function which can reset the directory handle initially created by opendir() function.
Syntax:
rewinddir(directory)
It accepts only the directory path as its input argument which is used to tell the directory handle resource path which was opened with opendir() previously. This is an optional parameter which if not specified then the previous link used by the opendir() will be considered.
Example:
<?php $direc = "/file/"; // To open the directory and read its contents if (is_dir($direc)){ if ($place = opendir($direc)){ // List files in images directory while (($file = readdir($place)) !== false){ echo "filename:" . $file . "\n"; } rewinddir(); echo("Using the function rewinddir\n"); // List files again while (($file = readdir($place)) !== false){ echo "filename:" . $file . "\n"; } closedir($place); echo("Closed directory"); } } ?>
Output:
In this example first, we are specifying the directory path and if statement we are using to verify if the directory path is present or not. If the directory is present then we are opening and reading the contents of the file and printing the same. Now the file handler will stop printing since it reached the end of file pointer. When we use the rewinddir() function it resets the file handler and hence when we print the directory contents it prints the same output again.
We have gone through some of the basic and important PHP directory functions commonly used in this article. We also noticed that a few of these functions are dependant on each other. For example, we cannot use readdir() without using opendir(). Few other functions which are used are dir(), scandir() and getcwd().
以上是PHP目录的详细内容。更多信息请关注PHP中文网其他相关文章!