douji8549 2014-01-22 20:03
浏览 23
已采纳

搜索文件夹上已知扩展名的未知文件名

How to search on folder if a know the extension of files but dont know the filename.

presume i have a folder with files like this.

folder/1.txt
folder/2.txt
folder/3.txt

using this simple method i echo all the files wich filenames contains only numbers.

  for ($i=0; $i<=3; $i++) {
  $files = 'folder/'. $i .'.txt';

  echo "$i.txt <br />";;     

and the above code echo something like this

 1.txt
 2.txt
 3.txt

is possible to achieve if filenames contains letters? or even better letters and numbers.

aaa.txt
asdwe.txt
1asw2.txt

is this possible?

  • 写回答

1条回答 默认 最新

  • duanboshi1472 2014-01-22 20:25
    关注

    Do that with 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 />';
      }
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么