如何用php读取一个文件夹下的文件个数? 看手册的 "XXIII. 目录函数库" 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是一个可以读取文件夹里文件信息的类,用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> ".$RESULT[$i]["folder"]."</td>"; echo "<td> ".$RESULT[$i]["file"]."</td>"; echo "<td> ".$RESULT[$i]["size"]."</td>"; if ($RESULT[$i]["time"]) { echo "<td>".strftime("%d.%m.%y - %H:%M",$RESULT[$i]["time"])."</td>"; } else { echo "<td> </td>"; } echo "</tr>\n"; } echo "</table>\n"; PHP小程序 求一正则表达式,匹配一个限定的id PHP支持联盟 论坛建立了 大家一起来交流哦 Apache Alias 映射局域网目录问题 问一下phplib的循环嵌套的问题. 最近困惑,迷茫过,不知道自己是否还要继续,如果要继续还需要补充什么知识? GD库生成动态多帧的gif图档 php和xml结合有什么好处? 【请教】PHP+ODBC+SQL Server数据库进行数据库操作的性能问题~~~ php4中的SESSION问题! 请问下面提示什么意思? 不被注意的问题
{ 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> ".$RESULT[$i]["folder"]."</td>";
echo "<td> ".$RESULT[$i]["file"]."</td>";
echo "<td> ".$RESULT[$i]["size"]."</td>";
if ($RESULT[$i]["time"])
{
echo "<td>".strftime("%d.%m.%y - %H:%M",$RESULT[$i]["time"])."</td>";
}
else
{
echo "<td> </td>";
}
echo "</tr>\n";
}
echo "</table>\n";