/** * 监视文件夹 * @param $host string 服务器地址 * @param $port int 服务器端口 * @param $root string 要监视的目录 * @param $ignore array 忽略的文件 * @return bool 是否被改变 */ function http_watch_dir($url, $root, $server_root, $ignore) { $changed = false; $modify_table = load_modify_time(); if (!is_dir($root)) { echo "{$root} not dir\n"; exit(1); } $queue = array($root); $t = -microtime(true); while (!empty($queue)) { // echo "queue\n"; var_dump($queue); $root_dir = array_shift($queue); // echo "enter dir $root_dir\n"; $d = opendir($root_dir); if ($d === false) { echo "{$root_dir} not exists\n"; exit(1); } while (($f = readdir($d)) !== false) { if ($f == '.' || $f == '..') { continue; } if (in_array($f, $ignore)) { // echo "skip $f\n"; continue; } $filename = "{$root_dir}/{$f}"; if (is_file($filename)) { $filesize = filesize($filename); assert($filesize !== false); if ($filesize > 100 * 1024 * 1024) { // big than 100M echo "skip {$filename} with size {$filesize}\n"; } else { $relat_path = substr(dirname($filename), strlen($root)); $changed = http_process_file($url, $filename, "{$server_root}/{$relat_path}", $changed); } } elseif (is_dir($filename)) { // echo "add to queue $filename\n"; $queue[] = "{$filename}"; } } save_modify_time(modify_time()); } $t += microtime(true); echo " ({$root} " . intval($t * 1000) . " ms)"; // echo "ok\n"; save_modify_time(modify_time()); return $changed; }
function modify_time($key = null, $value = null) { static $modify_table; if (empty($modify_table)) { $modify_table = load_modify_time(); } if ($key === null) { return $modify_table; } if (PHP_OS === 'WINNT') { $key = iconv('GBK', 'UTF-8', $key); } if ($value === null) { return isset($modify_table[$key]) ? $modify_table[$key] : null; } $modify_table[$key] = $value; }