Home >Backend Development >PHP Problem >How to query files in a directory with php
How to query files in a directory with php: 1. Create a PHP sample file; 2. Get the upper-level directory of the current file; 3. Scan all files in the $con directory through "scandir($con);" Just file.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How does php query the files in the directory?
PHP Get all the files in the current directory
We sometimes want to get all the file names in the current directory. The following is a method I wrote, please refer to it
// 获取当前文件的上级目录 $con = dirname(__FILE__); // 扫描$con目录下的所有文件 $filename = scandir($con); // 定义一个数组接收文件名 $conname = array(); foreach($filename as $k=>$v){ // 跳过两个特殊目录 continue跳出循环 if($v=="." || $v==".."){continue;} //截取文件名,我只需要文件名不需要后缀;然后存入数组。如果你是需要后缀直接$v即可 $conname[] = substr($v,0,strpos($v,".")); }
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to query files in a directory with php. For more information, please follow other related articles on the PHP Chinese website!