簡體   English   中英

Scandir:檢查具有相同名稱的文件(擴展名除外)

[英]Scandir: Checking for files with the same name (excluding extension)

是否可以檢查具有相同文件名(僅文件擴展名不同)且僅顯示其名稱一次的文件?

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

        }
    }
}
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包含唯一的文件名

嘗試這個

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

    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM