PHP目录
PHP 目录函数顾名思义是一组用于检索详细信息、修改它们以及获取有关各种文件系统目录及其特定内容的信息的函数。可以对目录执行很多操作,例如创建、删除、更改当前工作目录、列出目录中存在的文件等。这些函数不需要单独安装,因为它们是 PHP 核心的一部分。但要启用 chroot() 功能,我们需要配置 –enable-chroot-func 选项。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
PHP目录的功能
让我们了解一些基本的 PHP 目录功能,如下所示:
1.创建一个新目录
我们使用 mkdir() 函数在 PHP 编程脚本中创建一个新目录。
语法:
mkdir($dir_path,$mode,$recursive_flag,$context);
哪里,
- $dir_path 是将创建指定的新目录的相对路径或绝对路径。
- $mode 是采用八进制值的参数,它确定新创建的目录的可访问级别。
- $recursive 是一个标志类型字段,有 2 个值 true 或 false,可以允许我们创建嵌套目录或不允许。
- $context 与 PHP unlink() 类似,例如使用流来指定某些协议等。这也将仅返回一个布尔值,如果执行成功完成,该值将为 true,否则为 false。
示例:
<?php mkdir("/articles/"); echo("Directory created"); ?>
输出:
这是一个基本示例,展示了在我们需要的路径中创建目录。确保路径有足够的权限,否则将抛出“权限被拒绝”错误。
2.列出目录的内容
我们分别使用 opendir() 和 readdir() 来打开目录链接并读取它。第 1 步是打开目录,第 2 步是读取它。
第 1 步: 要打开目录链接,opendir() 是我们用来执行此步骤的函数。它需要两个输入参数,如下所示。
语法:
opendir($dir_path,$context);
- $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 语句中检查该目录是否存在,然后继续打开该目录的内容并读取。输出显示目录中存在的文件名。
3.关闭目录
我们使用 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 条件语句检查路径是否有效,如果有效,则打开目录,读取其变量,然后关闭它。因此,在目录的打开和关闭之间可以进行任何操作。
4.更改当前目录
我们使用函数 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.
5. To Change the Directory Path of Root
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.
6. To Reset the Directory Handle
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.
Conclusion
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中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP 8.4 带来了多项新功能、安全性改进和性能改进,同时弃用和删除了大量功能。 本指南介绍了如何在 Ubuntu、Debian 或其衍生版本上安装 PHP 8.4 或升级到 PHP 8.4

Visual Studio Code,也称为 VS Code,是一个免费的源代码编辑器 - 或集成开发环境 (IDE) - 可用于所有主要操作系统。 VS Code 拥有针对多种编程语言的大量扩展,可以轻松编写

JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

本教程演示了如何使用PHP有效地处理XML文档。 XML(可扩展的标记语言)是一种用于人类可读性和机器解析的多功能文本标记语言。它通常用于数据存储

静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

字符串是由字符组成的序列,包括字母、数字和符号。本教程将学习如何使用不同的方法在PHP中计算给定字符串中元音的数量。英语中的元音是a、e、i、o、u,它们可以是大写或小写。 什么是元音? 元音是代表特定语音的字母字符。英语中共有五个元音,包括大写和小写: a, e, i, o, u 示例 1 输入:字符串 = "Tutorialspoint" 输出:6 解释 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。总共有 6 个元

PHP的魔法方法有哪些?PHP的魔法方法包括:1.\_\_construct,用于初始化对象;2.\_\_destruct,用于清理资源;3.\_\_call,处理不存在的方法调用;4.\_\_get,实现动态属性访问;5.\_\_set,实现动态属性设置。这些方法在特定情况下自动调用,提升代码的灵活性和效率。

PHP和Python各有优势,选择依据项目需求。1.PHP适合web开发,尤其快速开发和维护网站。2.Python适用于数据科学、机器学习和人工智能,语法简洁,适合初学者。
