sitemap站点地图

<?php
$root = '/home/webroot/cmsnew.com/html/hk'; //当前目录
$dir_array = array('news','stocks', 'shhkc', 'warrants', 'ipo', 'ashares', 'futures', 'forex', 'commodity', 'events');
//if(isset($argv) && count($argv)!==0){
//    $dir_array = $argv[1];
//}

list_file($root,$dir_array);

function list_file($root,$dir_array)
{
    foreach ($dir_array as $dir) {//遍历
        $file_location = $root .'/'. $dir;//生成路径
        //$file = '/home/xiongx/sitemap' . '/' .$dir. '.xml';
        //todo 修改为正式要生成的地址
        $file = '/home/webroot/cmsnew.com/html/hk/sitemap' . '/' .$dir. '.xml';
        if(file_exists($file)){
            @unlink($file);
            $realname = basename($file,'.xml');
            for($i=1;$i<=9;$i++){
                $newname = $realname . $i;
                @unlink(str_replace($realname,$newname,$file));
            }
        }
        file_put_contents($file,"<urlset>",FILE_APPEND);
        add_site($file_location,$file);
        file_put_contents($file,"</urlset>",FILE_APPEND);
    }
}
function add_site($dir,&$file){
    if(preg_match('/^[a-z]{1,30}$/i',substr($dir,strripos($dir,'/')+1))) {
        $content = "<url><loc>" . "http://www.cnfol.hk/" . str_replace('/home/webroot/cmsnew.com/html/hk/', '', $dir) . "</loc></url>";
        file_put_contents($file,$content,FILE_APPEND);
    }
    $dir_arr = scandir($dir);
    array_shift($dir_arr);
    array_shift($dir_arr);
    foreach($dir_arr as $v){
        if(is_dir($dir . '/' . $v)){
            add_site($dir . '/' . $v,$file);
        }elseif(is_file($dir . '/' . $v) && preg_match('/(\.shtml)$/i',$dir . '/' . $v)){
            $content = "<url><loc>" . "http://www.cnfol.hk/" . str_replace('/home/webroot/cmsnew.com/html/hk/', '', $dir. '/' . $v) . "</loc></url>";
            file_put_contents($file,$content,FILE_APPEND);
            $filesize=abs(filesize($file));
            if($filesize>5120000){
                file_put_contents($file,"</urlset>",FILE_APPEND);
                $realname = basename($file,'.xml');
                $n = (int)$realname + 1 ;
                $newname = $realname . $n;
                $file = str_replace($realname,$newname,$file);
                file_put_contents($file,"<urlset>",FILE_APPEND);
            }
        }
    }

}

  

posted @ 2017-06-19 23:17  王芬老师  阅读(106)  评论(0编辑  收藏  举报