我使用以下内容在"html/"和链接路径中创建文件列表。
当我查看阵列时,它会显示,例如my_file_name.php
如何使数组只显示文件名而不显示扩展名?
$path = array("./html/","./link/");
$path2= array("http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/html/","http://".$_SERVER['SERVER_NAME'].dirname($_SERVER["PHP_SELF"])."/link/");
$start="";
$Fnm = "./html.php";
$inF = fopen($Fnm,"w");
fwrite($inF,$start."n");
$folder = opendir($path[0]);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$folder2 = opendir($path[1]);
$imagename ='';
while( $file2 = readdir($folder2) ) {
if (substr($file2,0,strpos($file2,'.')) == substr($file,0,strpos($file,'.'))){
$imagename = $file2;
}
}
closedir($folder2);
$result="<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active">n<a href="$file">n$file2n</a><span class="glow"><br></span>
</li>n";
fwrite($inF,$result);
}
}
fwrite($inF,"");
closedir($folder);
fclose($inF);
pathinfo()
很好,但我认为在这种情况下,您可以使用strrpos()
。我不确定你想用$imagename
做什么,但我把它留给你。以下是您可以使用代码来比较基本文件名的方法:
// ...
$folder = opendir($path[0]);
while( $file = readdir($folder) ) {
if (($file != '.')&&($file != '..')&&($file != 'index.htm')) {
$folder2 = opendir($path[1]);
$imagename ='';
$fileBaseName = substr($file,0,strrpos($file,'.'));
while( $file2 = readdir($folder2) ) {
$file2BaseName = substr($file2,0,strrpos($file2,'.'));
if ($file2BaseName == $fileBaseName){
$imagename = $file2;
}
}
closedir($folder2);
$result="<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active">n<a href="$file">n$file2n</a><span class="glow"><br></span>
</li>n";
fwrite($inF,$result);
}
}
我希望这能有所帮助!