In the previous article:PHP – Delete all files and directories in the current directory.", most files can be deleted! However, you may encounter an issue: the introductory article cannot be resolved! Let's analyze the reason!
glob ` scandir()` is a universal file system function that returns the names of files or directories matching a specified pattern. Use it in PHP. glob When using the function, you may notice that it cannot locate items containing a dot.(.)The initial document (e.g..DS_Store). This is because files that start with a dot belong to the `class`. UNIX The system considers ithidden fileIt will not be displayed by conventional file system operations. Therefore, when you use it glob When searching the file system, the system by default hides files that start with a dot and does not return these file names. If you wish to find files that start with a dot, you can use the following method:
- Display search: Include file names that start with a dot in the glob function pattern (e.g., „. "or „.DS_Store"). This will return files that start with a dot, as well as all other files.
- Use an iterator: You can use the DirectoryIterator or RecursiveDirectoryIterator classes from the PHP SPL (Standard PHP Library) to iterate through a directory, and then call the getFilename() method to retrieve all file names, including those that start with a dot.
$dir = new RecursiveDirectoryIterator(__DIR__);
$iterator = new RecursiveIteratorIterator($dir);
$files = array();
foreach ($iterator as $file) {
if (!$file->isDir() && $file->getFilename()[0] === '.') {
$files[] = $file->getPathname();
}
}
print_r($files);