Php 搜索文件夹上具有已知扩展名的未知文件名
如果知道文件的扩展名但不知道文件名,如何搜索文件夹 假设我有一个文件夹,里面有这样的文件Php 搜索文件夹上具有已知扩展名的未知文件名,php,Php,如果知道文件的扩展名但不知道文件名,如何搜索文件夹 假设我有一个文件夹,里面有这样的文件 folder/1.txt folder/2.txt folder/3.txt 使用这个简单的方法,我回显所有文件名只包含数字的文件 for ($i=0; $i<=3; $i++) { $files = 'folder/'. $i .'.txt'; echo "$i.txt <br />";; 如果文件名包含字母,是否可以实现?或者更好的字母和数字 aaa.txt
folder/1.txt
folder/2.txt
folder/3.txt
使用这个简单的方法,我回显所有文件名只包含数字的文件
for ($i=0; $i<=3; $i++) {
$files = 'folder/'. $i .'.txt';
echo "$i.txt <br />";;
如果文件名包含字母,是否可以实现?或者更好的字母和数字
aaa.txt
asdwe.txt
1asw2.txt
这可能吗?使用DirectoryIterator执行此操作:
<?php
const DS = DIRECTORY_SEPARATOR;
$dir = new DirectoryIterator('your' . DS . 'direcory' . DS);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
// relative url of file = $fileinfo->getPathname();
// file name = $fileinfo->getFilename();
echo $fileinfo->getFilename() . '<br />';
}
}
1。在目录中的文件上循环。2.使用substr
或类似函数获取文件名的最后4个字符。3.检查最后的字符是否与文件扩展名匹配。如果你在谷歌上搜索你的问题标题,或者只使用$files=glob('*.txt')
:@SergiuParaschiv在哪里添加文件夹名$files=glob('*.txt')
在哪里执行wich文件夹中的搜索?我想在名为folder
的文件夹中搜索。谢谢
<?php
const DS = DIRECTORY_SEPARATOR;
$dir = new DirectoryIterator('your' . DS . 'direcory' . DS);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
// relative url of file = $fileinfo->getPathname();
// file name = $fileinfo->getFilename();
echo $fileinfo->getFilename() . '<br />';
}
}