![](/img/trans.png)
[英]how to get a list of files with .html extension using PHP 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.