0

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

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 />';         

        }
    }
}
4

2 回答 2

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 />';         

    }
}
}
于 2013-02-28T22:56:06.353 回答
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;
}

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

于 2013-02-28T22:50:19.590 回答