0

I'm using the following to create a list of my files in the 'html/' and link path.

When I view the array it shows, for example, my_file_name.php

How do I make it so the array only shows the filename and not the extension?

$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);
  • 1
    perhaps you could look into `pathinfo()` – Ja͢ck Aug 27 '12 at 18:43
  • Firstly, I would use http://php.net/manual/en/class.directoryiterator.php And Jack is right about pathinfo(), +1. Array ( [dirname] => /www/htdocs [basename] => index.html [extension] => html [filename] => index ) – wesside Aug 27 '12 at 18:45
  • can you explain further by giving an example with reference to above code ? – user1613566 Aug 27 '12 at 18:52

1 Answers1

0

pathinfo() is good, but I think in this case you can get away with strrpos(). I'm not sure what you're trying to do with $imagename, but I'll leave that to you. Here is what you can do with your code to compare just the base filenames:

// ...
$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);
       }
}

I hope that helps!

Spudley
  • 166,037
  • 39
  • 233
  • 307
MorganGalpin
  • 685
  • 6
  • 7