【问题标题】:Get file name if have part of it - PHP [duplicate]获取文件名(如果有一部分)-PHP [重复]
【发布时间】:2013-07-06 10:20:18
【问题描述】:

好的,所以,如果我有部分文件名,我需要从目录中获取文件名。示例:

directory/
 - filename1.dat1
 - otherfile2.dat2
 - thirdfile.dat3

如果我有扩展名(dat1,dat2,...),我想获取文件名。 如果你不能理解我,我有“dat3”并且想要获取包含该扩展名的文件名。

第二个例子:

<?php
   $dat_extension = "dat3";
   $file_name = getFileName($dat_extension); // something like this
   echo $file_name; // returns thirdfile.dat3
 ?>

【问题讨论】:

  • glob() 函数怎么样?
  • 那么fnmatch()函数呢?

标签: php regex


【解决方案1】:

试试这个:

$dir = "directory";

foreach(glob($dir . '/*.dat3') as $file) 
{
    echo $file;
}

【讨论】:

    【解决方案2】:

    试试这个:

    <?php
    $array = array();
    $files = scandir('dir/');
    foreach ($files as $file) {
        $ext = pathinfo($file, PATHINFO_EXTENSION);
        if($ext == 'dat3'){
            array_push($array, $file);
        }
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-18
      • 2010-11-26
      • 1970-01-01
      • 1970-01-01
      • 2022-12-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      相关资源
      最近更新 更多