Webサイトの更新情報を自動的に取得するスクリプト【とりあえず完成版】
サイト内にあるファイルの更新情報を取得し、DBに格納するスクリプトです。
すでにそのファイルの更新日時が存在する場合、更新日時を更新。存在しない場合はあらたにinsertする仕様です。
今回からAtomというエディタを使い始めて、かなりコーディングが楽になりました。
※間違いがあったのでこそっと直しました。
<?php date_default_timezone_set('Asia/Tokyo'); $dnsinfo="mysql:dbname=site;host=localhost;charset=utf8"; $user="siteMaster"; $pw="site"; $startDir = "./"; if(!is_dir($startDir)){ exit; } try{ $pdo = new PDO($dnsinfo,$user,$pw); scanningDirectory($pdo,$startDir); }catch(PDOException $e){ echo $e->getMessage(); } function scanningDirectory($pdo,$dir){ if(is_dir($dir)){ $dh = opendir($dir); }else{ return; } while(($file = readdir($dh))!== false){ if($file == '.' || $file == '..'){ continue; } $path = $dir.$file; if (is_dir($path)) { $path = $path."/"; scanningDirectory($pdo,$path); }else{ $path = $dir.$file; updateDate($pdo,$path); } } } function updateDate($pdo,$path){ $stats = stat($path); $updateDate = date('YmdHis',$stats['mtime']); $countCheckQuery = "select count(*) from MONITOR_PAGE_TEST where path=?"; $stmt = $pdo->prepare($countCheckQuery); $stmt->execute(array($path)); while($countCheck = $stmt->fetch(PDO::FETCH_ASSOC)){ $count = $countCheck['count(*)']; } if( $count == 1 ){ $updateQuery = "update MONITOR_PAGE_TEST set update_date = ? where path = ?"; $stmt = $pdo->prepare($updateQuery); $stmt->execute(array($updateDate,$path)); }else if ($count == 0){ $insertQuery = "insert into MONITOR_PAGE_TEST values(?,?)"; $stmt = $pdo->prepare($insertQuery); $stmt->execute(array($path,$updateDate)); }else{ echo "invalid State of Database."; } } ?>