2013-02-28 107 views
0

是否可以检查具有相同文件名(仅在文件扩展名中不同)的文件,并且只显示一次它们的名称?Scandir:检查具有相同名称的文件(不包括扩展名)

if (is_dir($dir_path)) { 

    $files = scandir($dir_path); 

    foreach($files as $file) { 

     if (!in_array($file, $exclude_all)) { 

      $path_to_file = $dir_path . $file; 
      $bare_name = pathinfo($path_to_file, PATHINFO_FILENAME); 
      $extension = pathinfo($path_to_file, PATHINFO_EXTENSION); 

      echo 'Path to file: ' . $path_to_file . '<br />'; 
      echo 'Bare file name: ' . $bare_name . '<br />'; 
      echo 'Extension: ' . $extension . '<br />';   

     } 
    } 
} 
+0

是的,这是可能的。 – 2013-02-28 22:48:01

+0

你能告诉我吗? ;) – Betsy 2013-02-28 22:49:30

回答

0
you can try this: 

//first save all files in an array 
$bare_name = pathinfo($path_to_file, PATHINFO_FILENAME); 
if(!in_array($bare_name, $files)){ 
$files[] = $bare_name; 
} 

现在$文件包含唯一的文件名

0

试试这个

if (is_dir($dir_path)) { 

$tmpBareNames = array(); 

$files = scandir($dir_path); 

foreach($files as $file) { 

    if (!in_array($file, $exclude_all)) { 

     $path_to_file = $dir_path . $file; 
     $bare_name = pathinfo($path_to_file, PATHINFO_FILENAME); 
     if(!in_array($bare_name,$tmpBareNames)) 
      $tmpBareName[] = $bare_name; 
     else break; 
     $extension = pathinfo($path_to_file, PATHINFO_EXTENSION); 

     echo 'Path to file: ' . $path_to_file . '<br />'; 
     echo 'Bare file name: ' . $bare_name . '<br />'; 
     echo 'Extension: ' . $extension . '<br />';   

    } 
} 
} 
相关问题