dao = $dao; } public function syncfile() { $list = $this->flattenArray($this->scanDirectory()); $this->dao->saveAll($list); } public function scanDirectory($dir = '') { if ($dir == '') $dir = root_path(); $result = array(); $files = scandir($dir); foreach ($files as $file) { if ($file != '.' && $file != '..') { $path = $dir . '/' . $file; $fileInfo = array( 'name' => $file, 'update_time' => date('Y-m-d H:i:s', filemtime($path)), 'create_time' => date('Y-m-d H:i:s', filectime($path)), 'path' => str_replace(root_path(), '', $dir), 'full_path' => str_replace(root_path(), '', $path), ); if (is_dir($path)) { $fileInfo['type'] = 'dir'; $fileInfo['contents'] = $this->scanDirectory($path); } else { $fileInfo['type'] = 'file'; } $result[] = $fileInfo; } } return $result; } public function flattenArray($arr) { $result = array(); foreach ($arr as $item) { $result[] = array( 'name' => $item['name'], 'type' => $item['type'], 'update_time' => $item['update_time'], 'create_time' => $item['create_time'], 'path' => $item['path'], 'full_path' => $item['full_path'], ); if (isset($item['contents'])) { $result = array_merge($result, $this->flattenArray($item['contents'])); } } return $result; } }