Directory processing function
The basic idea of processing folders is as follows:
1. When reading a certain path, determine whether it is a folder
2. If it is a folder, open the specified folder and return Resource variables of the file directory
3. Use readdir to read the files in the directory once, and the directory pointer is shifted backward once
4. Use readdir to read to the end, and there is no readable file Return false
5. Close the file directory
Let’s learn a list of common functions:
##
<?php
//设置打开的目录是D盘
$dir = "d:/";
//判断是否是文件夹,是文件夹
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
//读取一次向后移动一次文件夹指针
echo readdir($dh).'<br />';
echo readdir($dh).'<br />';
echo readdir($dh).'<br />';
echo readdir($dh).'<br />';
//读取到最后返回false
//关闭文件夹资源
closedir($dh);
}
}
?>
即然是读取一次向后移动一次,我们是不是可以
<?php
//设置打开的目录是D盘
$dir = "d:/";
//判断是否是文件夹,是文件夹
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
//读取到最后返回false,停止循环
while (($file = readdir($dh)) !== false) {
echo "文件名为: $file : 文件的类型是: " . filetype($dir . $file) . "<br />";
}
closedir($dh);
}
}
?>
new file
<?php
//设置打开的目录是D盘
$dir = "d:/";
//判断是否是文件夹,是文件夹
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
//读取一次向后移动一次文件夹指针
echo readdir($dh).'<br />';
echo readdir($dh).'<br />';
echo readdir($dh).'<br />';
echo readdir($dh).'<br />';
//读取到最后返回false
//关闭文件夹资源
closedir($dh);
}
}
?>
即然是读取一次向后移动一次,我们是不是可以
<?php
//设置打开的目录是D盘
$dir = "d:/";
//判断是否是文件夹,是文件夹
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
//读取到最后返回false,停止循环
while (($file = readdir($dh)) !== false) {
echo "文件名为: $file : 文件的类型是: " . filetype($dir . $file) . "<br />";
}
closedir($dh);
}
}
?>
Preview
Clear
- Course Recommendations
- Courseware download
The courseware is not available for download at the moment. The staff is currently organizing it. Please pay more attention to this course in the future~
Students who have watched this course are also learning
Let's briefly talk about starting a business in PHP
Quick introduction to web front-end development
Large-scale practical Tianlongbabu development of Mini version MVC framework imitating the encyclopedia website of embarrassing things
Getting Started with PHP Practical Development: PHP Quick Creation [Small Business Forum]
Login verification and classic message board
Computer network knowledge collection
Quick Start Node.JS Full Version
The front-end course that understands you best: HTML5/CSS3/ES6/NPM/Vue/...[Original]
Write your own PHP MVC framework (40 chapters in depth/big details/must read for newbies to advance)
















