0

我正在使用以下内容在“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$file2\n</a><span class=\"glow\"><br></span>
</li>\n";
        fwrite($inF,$result);
       }
}
fwrite($inF,"");
closedir($folder);

fclose($inF);
4

1 回答 1

0

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$file2\n</a><span class=\"glow\"><br></span>
</li>\n";
        fwrite($inF,$result);
       }
}

我希望这会有所帮助!

于 2012-08-27T20:29:47.633 回答