代码记录(关于定时扫描某个XML文件并插入数据表)
抱歉,各位看官,这是给自己看的,所以没有太多的注释。
<?php
??? include('/jtxm/web/config/config.php');???
??? //根目录
??? $root?????????? = '/jtxm/server/';
??? $petDir???????? = '/common/item/xml/';
??? //更新宠物名字的日志
??? $petName_update_log = '/jtxm/web/update/petName_update_log.php';
??? //获取更新目录确定是否需要更新
??? $updateInfo?????? = updateDir($root);
??? $petPath????????? = $root.$updateInfo['updateDir'].$petDir;
??? $update_log?????? = '';
???
??? if(file_exists($petName_update_log)){
??? ??? if(filemtime($petName_update_log)<$updateInfo['updateTime']){
??? ??? ??? $update_log .= update_petName($petPath);
??? ??? }else{
??? ??? ??? exit;
??? ??? }
??? }else{
??? ??? $update_log .= update_petName($petPath);
??? }
??? file_put_contents($petName_update_log,$update_log);
???
??? function update_petName($petPath){
??? ??? $petFile = "pet.xml";
??? ??? $conn = mysql_connect(DB_HOST,DB_USER,DB_PWD);
??? ??? mysql_select_db(DB_WEB);
??? ??? mysql_query('set names utf8');
??? ??? $sql = "TRUNCATE TABLE `pet_name`";
??? ??? $result = mysql_query($sql);
??? ??? $update_log = '';
??? ??? if($result){
??? ??? ??? $update_log .= '清空表成功!';
??? ??? ??? $pet_file??? = $petPath.$petFile;
??? ??? ??? if(file_exists($pet_file)){
??? ??? ??? ??? $xml = new DOMDocument();?
??? ??? ??? ??? $xml->load($pet_file);
??? ??? ??? ??? $nodes= $xml->getElementsByTagName('Item');
??? ??? ??? ??? foreach($nodes as $node){
??? ??? ??? ??? ??? $id???? = $node->getAttribute('id');
??? ??? ??? ??? ??? $name?? = $node->getAttribute('name');
??? ??? ??? ??? ??? $incsql = "insert into `pet_name`(`id`,`name`) values('".$id."','".$name."')";
??? ??? ??? ??? ??? mysql_query($incsql);
??? ??? ??? ??? }
??? ??? ??? ??? $update_log .= $pet_file.':更新成功!\n';
??? ??? ??? }else{
??? ??? ??? ??? $update_log .= $pet_file.':不存在!\n';
??? ??? ??? }
??? ??? }else{
??? ??? ??? $update_log .= '清空表失败!';
??? ??? }
??? ??? return $update_log;
??? }
???
??? //查找最新目录
??? function updateDir($Dir){
??? ??? $dirs=??? scandir($Dir);
??? ??? //确定最新目录
??? ??? $updateDir['updateTime']= 0;
??? ??? $updateDir['updateDir'] = '';
??? ??? foreach($dirs as $dir){
??? ??? ??? if(is_dir($Dir.$dir) && $dir <>'.' && $dir<>'..' && ereg('r[0-9]{8}',$dir)){
??? ??? ??? ??? $f_mtime???? = filemtime($Dir.$dir);
??? ??? ??? ??? if($f_mtime? > $updateDir['updateTime']){
??? ??? ??? ??? ??? $updateDir['updateTime'] = $f_mtime;
??? ??? ??? ??? ??? $updateDir['updateDir']? = $dir;
??? ??? ??? ??? }
??? ??? ??? }
??? ??? }
??? ??? return $updateDir;
??? }
?>
?
?
?
?
?
?
?
?
?
<?php
header("content-type:text/html; charset=utf-8");
//根目录
$root???????????? = '/jtxm/server/';
$questDir???????? = '/common/item/xml/';
//更新宠物名字的日志
$petName_update_log = '/jtxm/web/update/petName_update_log.php';
//获取更新目录确定是否需要更新
$updateInfo?????? = updateDir($root);
$questPath??????? = $root.$updateInfo['updateDir'].$questDir;
if (file_exists($questPath.'/pet.xml')) {
??? $xml = simplexml_load_file($questPath.'/pet.xml');
??? include('/jtxm/web/config/config.php');
??? $conn = mysql_connect(DB_HOST,DB_USER,DB_PWD);
??? mysql_select_db(DB_WEB);
??? mysql_query('set names utf8');
??? $sql = "TRUNCATE TABLE pet_name";
??? $rs = mysql_query($sql) or die(mysql_error());
??? if(!$rs){
??? ??? ??? $this->error();
??? }else{
??? ??? $update_log="清空数据表成功 \t";
??? }
??? foreach($xml->Item as $item)
??? {
??? ?? $id=$item["id"];
??? ?? $name=$item["name"];
??? ?? $sql="insert into pet_name (id,name) values ($id,'$name')";
??? ?? $rs=mysql_query($sql) or die("查询错误". mysql_error());
??? ??? if(!$rs){
??? ??? ??? error();
??? ??? }
??? }
??? $update_log .= $questPath."更新成功\n";
} else {
??? $update_log ='没有找到pet.xml这个文件\n';
}
if(file_exists($petName_update_log)){
??? if(filemtime($petName_update_log)<$updateInfo['updateTime']){
??? ??? file_put_contents($petName_update_log,$update_log,FILE_APPEND);
??? }else{
??? ??? exit(0);
??? }
}
else{
??? $update_log ="没有找到".$petName_update_log."这个文件\n";
??? file_put_contents($petName_update_log,$update_log);
}???
//查找最新目录
function updateDir($Dir){
??? $dirs=??? scandir($Dir);
??? //确定最新目录
??? $updateDir['updateTime']= 0;
??? $updateDir['updateDir'] = '';
??? foreach($dirs as $dir){
??? ??? if(is_dir($Dir.$dir) && $dir <>'.' && $dir<>'..' && ereg('r[0-9]{8}',$dir)){
??? ??? ??? $f_mtime???? = filemtime($Dir.$dir);
??? ??? ??? if($f_mtime? > $updateDir['updateTime']){
??? ??? ??? ??? $updateDir['updateTime'] = $f_mtime;
??? ??? ??? ??? $updateDir['updateDir']? = $dir;
??? ??? ??? }
??? ??? }
??? }
??? return $updateDir;
}
function error(){
??? mysql_close($this->conn) or die("数据有误");
??? $update_log='数据有误\n';
}
?>