php归获取当前目录下的二级目录数 和文件数
<?php
header('Content-Type: text/html; charset=gb2312');
// $baseDir = "/www/users/";
$baseDir = "./cronjob";
$secondDir = exec("find ".$baseDir." -maxdepth 1 -type d",$dirList);
// $file_path="/www/prima/htdocs/apps/file_num.txt";
$file_path="/www/prima/htdocs/file_num.txt";
/* unlink($file_path); */
foreach( $dirList as $k => $v ){
if( $v!=$baseDir ){
read_all_dir($v,$baseDir);
}
}
function read_all_dir ( $dir,$baseDir )
{
$result = array();
$handle = opendir($dir);
$sitefilenum=mysql_connect("203.158.18.63","sitefilenum","sitefilenum123ewq");
mysql_query("set names 'gb2312'");
mysql_select_db("sitefilenum",$sitefilenum);
if ( $handle )
{
$file_command = exec("ls ".$dir." -alR|grep '^-'|wc -l",$file_num);
$dir_command = exec("ls ".$dir." -alR|grep '^d'|wc -l",$dir_num);
exec("ifconfig eth0 | grep 'inet addr' | awk '{ print $2}' | awk -F: '{print $2}'",$serIP);
if ($serIP[0]==""){
exec("ifconfig eth1 | grep 'inet addr' | awk '{ print $2}' | awk -F: '{print $2}'",$serIP);
}
$time = date("Y-m-d H:i:s",time());
closedir($handle);
$secondDir = explode("/",$dir);
$limit_sql = 'SELECT * FROM filenum WHERE domain = "'.end($secondDir).'"';
$limit_res= mysql_query($limit_sql,$sitefilenum);
$limit_cou=mysql_fetch_array($limit_res);
if(!empty($limit_cou)){
$sql = 'UPDATE filenum SET file_num="'.$file_num[0].'",dir_num="'.$dir_num[0].'",ip="'.$serIP[0].'",time="'.$time.'" WHERE domain="'.end($secondDir).'";'."\r\n";
}else{
$sql ='INSERT INTO filenum(domain,file_num,dir_num,ip,time) values("'.end($secondDir).'","'.$file_num[0].'","'.$dir_num[0].'","'.$serIP[0].'","'.$time.'");'."\r\n";
}
$file = fopen("./file_num.txt","a+");
fwrite($file,$sql);
fclose($file);
echo "#";
}
}
function insert_data ($file_path)
{
$sitefilenum=mysql_connect("203.158.18.63","sitefilenum","sitefilenum123ewq");
mysql_query("set names 'gb2312'");
mysql_select_db("sitefilenum",$sitefilenum);
$truncate_sql = "TRUNCATE TABLE filenum";
mysql_query($truncate_sql);
$content=file_get_contents($file_path);
$sql_array = explode(";",$content);
foreach($sql_array as $val_sql){
mysql_query($val_sql);
}
}
insert_data ($file_path);
?>