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中文网其他相关文章!

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。

CSP重要因为它能防范XSS攻击和限制资源加载,提升网站安全性。1.CSP是HTTP响应头的一部分,通过严格策略限制恶意行为。2.基本用法是只允许从同源加载资源。3.高级用法可设置更细粒度的策略,如允许特定域名加载脚本和样式。4.使用Content-Security-Policy-Report-Only头部可调试和优化CSP策略。

HTTP请求方法包括GET、POST、PUT和DELETE,分别用于获取、提交、更新和删除资源。1.GET方法用于获取资源,适用于读取操作。2.POST方法用于提交数据,常用于创建新资源。3.PUT方法用于更新资源,适用于完整更新。4.DELETE方法用于删除资源,适用于删除操作。

HTTPS是一种在HTTP基础上增加安全层的协议,主要通过加密数据保护用户隐私和数据安全。其工作原理包括TLS握手、证书验证和加密通信。实现HTTPS时需注意证书管理、性能影响和混合内容问题。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

Dreamweaver CS6
视觉化网页开发工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

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

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境