Is it possible to check for files with the same file name (which only differ in the file extension) and only display their names once?
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;
}
Now $files contains unique filename
Try this
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 />';
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.