How to read all file names in php

王林
Release: 2023-02-25 19:26:02
Original
5102 people have browsed it

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');
Copy after login

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; } }
Copy after login

3. Close the directory

Code sample:

closedir($handler);
Copy after login

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!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!