Home>Article>Backend Development> How to read all file names in php

How to read all file names in php

王林
王林 Original
2019-10-08 09:49:05 5117browse

How to read all file names in php

php gets all the file names in the directory

1. Open the directory handle of the directory to be operated

Code example:

//打开当前目录下的目录pic下的子目录common。 $handler = opendir('pic/common');

2. Loop through all files in the directory

Code example:

/*其中$filename = readdir($handler) 每次循环时将读取的文件名赋值给$filename,$filename !== false。 一定要用!==,因为如果某个文件名如果叫'0′,或某些被系统认为是代表false,用!=就会停止循环 */ while( ($filename = readdir($handler)) !== false ) { //略过linux目录的名字为'.'和‘..'的文件 if($filename != “.” && $filename != “..”) { //输出文件名 echo $filename; } }

3. Close the directory

Code sample:

closedir($handler);

Recommended tutorial:PHP video tutorial

The above is the detailed content of How to read all file names in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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