Home Backend Development PHP Problem What is the use of php directory operation function?

What is the use of php directory operation function?

May 21, 2021 am 09:28 AM
php

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.

What is the use of php directory operation function?

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(&#39;*.txt&#39;)); //glob()查找的是当前目录下的匹配的
//print_r(glob(&#39;./article/*.txt&#39;));//OK
 
//glob()在工作中很有用,但是今天重点不是
//为了查看目录下的所有文件,用opendir
/*
opendir   打开目录,返回资源句柄,可用于之后的 closedir(),readdir() 和 rewinddir() 调用中。
readdir   读取目录 \,成功则返回文件名 或者在失败时返回 FALSE ,因此我用NULL是不对的
mkdir 创建目录
rmdir 删除目录
closedir   关闭目录
is_dir 判断是否是目录
unlink 删除文件
*/
$path = &#39;./misc&#39;;
 
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(&#39;./misc/&#39;.$filename)) {
echo "是目录";
}
echo "<br />"; 
}
 
//创建
foreach (array(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;) as $v) {
$path = &#39;./misc/&#39;.$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(&#39;a&#39;, &#39;c&#39;, &#39;e&#39;) as $v) {
$path = &#39;./misc/&#39;.$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);
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles