看手册的 "XXIII. 目录函数库"

解决方案 »

  1.   

    这是一个可以读取文件夹里文件信息的类,用count($result)就可以取得文件个数啦class myDIR
    {    var $mask   = "";
        var $find   = "";
        var $root   = array();
        var $temp   = array();
        var $result = array();     //** setFIND ********************************************************************************//    function setFIND($val)
        {
            $this->find = $val;
        }    //** setMASK ********************************************************************************//    function setMASK($val="")
        {
            $this->mask = $val;
        }    //** setROOT ********************************************************************************//    function setROOT($val="")
        {
            $this->root[] = $val;
        }    //********************************************************************************************//    //** setARRAY ********************************************************************************//    function setARRAY($folder="",$file="",$size="",$time="")
        {
            $this->result[] = array("folder"=>$folder,"file"=>$file,"size"=>$size,"time"=>$time);
        }    //** doSEARCH ********************************************************************************//    function doSEARCH()
        {
            for($i=0; $i<count($this->root); $i++)
            {
                unset($this->temp);            $handle = @opendir($this->root[$i]);
                while ($file = @readdir ($handle))
                {
                    if (eregi("^\.{1,2}$",$file))
                    {
                        continue;
                    }
                    $this->temp[] = $this->root[$i]."/$file";
                }
                @closedir($handle);            if (count($this->temp) > 0)
                {
                    natcasesort($this->temp);                foreach ($this->temp as $val)
                    {
                        switch ($this->find)
                        {
                         case "folder":
                             $this->doFOLDER($val);
                         break;
                         case "files":
                             $this->doFILES($val);
                         break;
                         case "all":
                             $this->doFILES($val);
                             $this->doFOLDER($val);
                         break;
                        }
                    }
                }
            }
        }    //** doFOLDER ********************************************************************************//    function doFOLDER($val)
        {
            if( is_dir($val) )
            {
                if ($this->find == "all")
                {
                    $this->root[] = $val;
                }
            }
        }    //** doFILES ********************************************************************************//    function doFILES($val)
        {
            if( is_file($val) && $this->doMATCH($val) )
            {
                $this->doINFO($val);
            }
        }    //** doINFO *********************************************************************************//    function doINFO($val)
        {
            $fSIZE = filesize($val);
            $fTIME = filemtime($val);        $offset = strrpos ($val, "/");
            $folder = substr ($val, 0, $offset);
            $file   = substr ($val, $offset+1);        $this->setARRAY($folder,$file,$fSIZE,$fTIME);
        }    //** getRESULT ********************************************************************************//    function doMATCH($file)
        {
         $mask = $this->mask;
            $mask = str_replace(".", "\.", $mask);
            $mask = str_replace("*", "(.*)", $mask);        $mask_array = explode(',',$mask);
            foreach ($mask_array as $valid)
            {
                if(eregi("^$valid", $file, $geek))
                {
                    return true;
                }
            }
        }    //** getRESULT ********************************************************************************//    function getRESULT()
        {        $version = split ("\.", phpversion());
            if ( $version < 4 )
            {
                echo "ERROR: phpMyAdmin does only works with PHP-Versions 4.0 or higher.\n<br>";
                echo "Your Version is (".phpversion().").";
                exit;
            }        $this->doSEARCH();     if ( !$this->result )
         {
             echo "ERROR: No Data or Folder does not exist (".$this->root.")";
             exit;
         }        return $this->result;    }    //*******************************************************************************************//}具体用发:
    require("./class.dir.php"); $DIR = new myDIR; $DIR->setMASK("*.php,*.html,*.txt"); // ("*.html,*.htm,*.txt") separated with comma
     $DIR->setFIND("files");              // could be "folder" "files" "all"
     $DIR->setROOT("./phpMyAdmin");       // start folder $RESULT = $DIR->getRESULT(); echo "<table cellpadding='5' cellspacing='5'>\n";
     echo "<tr><td colspan='4'>Found ".count($RESULT)."</td></tr>\n";
     for ( $i=0 ; $i < count($RESULT); $i++ )
     {
         $bgcolor = ( $i%2 == 0 ) ? "#DDDDDD" : "#EFEFEF";     echo "<tr bgcolor='$bgcolor'>";
         echo "<td>&nbsp;".$RESULT[$i]["folder"]."</td>";
         echo "<td>&nbsp;".$RESULT[$i]["file"]."</td>";
         echo "<td>&nbsp;".$RESULT[$i]["size"]."</td>";
         if ($RESULT[$i]["time"])
         {
             echo "<td>".strftime("%d.%m.%y - %H:%M",$RESULT[$i]["time"])."</td>";
         }
         else
         {
             echo "<td>&nbsp;</td>";
         }
         echo "</tr>\n";
     }
     echo "</table>\n";