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

What is the use of php directory operation function?

藏色散人
Release: 2023-03-09 20:16:01
Original
1924 people have browsed it

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!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template