What is the use of php directory operation function?
The functions of php directory operation functions: 1. The opendir function is used to open the directory; 2. The readdir function is used to read the directory; 3. The mkdir function is used to create the directory; 4. The rmdir function is used to delete the directory, etc. wait.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
Explanation of functions related to directory operation of PHP
This article is suitable for beginners who are new to PHP to have a relevant understanding of directory operation functions.
<?php /** 目录相关操作函数 之前我们知道article下有a.txt b.txt c.txt d.txt 我们才去写 但是当文件下有e.html f.mp3.。。。我们只要处理txt文件 使用glob() 匹配文件 ****/ //print_r(glob('*.txt')); //glob()查找的是当前目录下的匹配的 //print_r(glob('./article/*.txt'));//OK //glob()在工作中很有用,但是今天重点不是 //为了查看目录下的所有文件,用opendir /* opendir 打开目录,返回资源句柄,可用于之后的 closedir(),readdir() 和 rewinddir() 调用中。 readdir 读取目录 \,成功则返回文件名 或者在失败时返回 FALSE ,因此我用NULL是不对的 mkdir 创建目录 rmdir 删除目录 closedir 关闭目录 is_dir 判断是否是目录 unlink 删除文件 */ $path = './misc'; var_dump(opendir($path));//resource(3, stream) $dh = opendir($path); /* echo readdir($dh); // . echo readdir($dh); // .. echo readdir($dh); // a.txt echo readdir($dh); // b.exe // . 和 .. 是虚拟目录,当前目录和上级目录, */ echo "<hr />"; //判断目录 while (($filename = readdir($dh)) !== false) { //直接判断真假是不对的,因为可能会出现0,因此使用false echo $filename; if (is_dir('./misc/'.$filename)) { echo "是目录"; } echo "<br />"; } //创建 foreach (array('a', 'b', 'c', 'd') as $v) { $path = './misc/'.$v; //意思是在misc下创建目录 if (file_exists($path) && is_dir($path)) {//先判断是否存在并且是目录 echo $path."已经存在<br />"; }else if (mkdir($path)) { echo $path."创建成功<br />";//第一次创建成功了,第二次创建就会失败,因为目录已经存在了 } else{ echo $path."创建失败<br />"; } } //删除目录 foreach (array('a', 'c', 'e') as $v) { $path = './misc/'.$v; if (file_exists($path) && is_dir($path)) { if (rmdir($path)) { //因为在linux下权限控制比较严格,所以要判断是否删除成功 echo $path."目录删除成功<br />"; //非空目录禁止删除,rmdir只能删除非空目录 }else{ echo $path."目录删除失败<br />"; } }else{ echo $path."目录不存在<br />"; } } //关闭目录 closedir($dh);
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the use of php directory operation function?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
