/** * Get files by search term * * @param string $path Path to search files * @param string $searchTerm Search term * @param array $result Result files and directory array * @param boolean $recursive True to search recursive * * @return array Files array by given search term */ public function getDirectoryTree($path = '', $searchTerm = '', &$result = array(), $recursive = false) { if (empty($path)) { return array(); } if (!is_dir($path)) { return array(); } if (empty($result)) { $result = array('dir' => array(), 'file' => array()); } $mediaArray = glob($path . '*'); foreach ($mediaArray as $media) { $mediaName = basename($media); if (!\FWSystem::detectUtf8($mediaName)) { $mediaName = utf8_encode($mediaName); } if (!empty($searchTerm) && !preg_match('/' . preg_quote($searchTerm) . '/i', $mediaName)) { continue; } $mediaType = is_dir($media) ? 'dir' : 'file'; $mediaPath = dirname($media); if ($mediaType == 'file' && !$this->isFileValidToShow($mediaPath, $mediaName)) { continue; } $result[$mediaType]['icon'][] = self::getFileTypeIconWebPath($media); $result[$mediaType]['name'][] = $mediaName; $result[$mediaType]['size'][] = $this->_getSize($media); $result[$mediaType]['type'][] = $this->_getType($media); $result[$mediaType]['date'][] = $this->_getDate($media); $result[$mediaType]['perm'][] = $this->_getPerm($media); $result[$mediaType]['path'][] = $mediaPath; } if ($recursive) { foreach (glob($path . '*', GLOB_ONLYDIR | GLOB_MARK) as $dir) { $this->getDirectoryTree($dir, $searchTerm, $result, $recursive); } } return $result; }
function _dirTree($path) { $dir = array(); $file = array(); $forbidden_files = array('.', '..', '.svn', '.htaccess', 'index.php'); if (is_dir($path)) { $fd = @opendir($path); $name = @readdir($fd); while ($name !== false) { if (!in_array($name, $forbidden_files)) { if (is_dir($path . $name)) { $dirName = $name; if (!\FWSystem::detectUtf8($dirName)) { $dirName = utf8_encode($dirName); } $dir['icon'][] = self::getFileTypeIconWebPath($path . $name); $dir['name'][] = $dirName; $dir['size'][] = $this->_getSize($path . $name); $dir['type'][] = $this->_getType($path . $name); $dir['date'][] = $this->_getDate($path . $name); $dir['perm'][] = $this->_getPerm($path . $name); } elseif (is_file($path . $name)) { // TODO // This won't work for .jpg thumbnails made from .png images and other // ways to create thumbnail file names. See the Image class. if (preg_match("/(?:\\.(?:thumb_thumbnail|thumb_medium|thumb_large)\\.[^.]+\$)|(?:\\.thumb)\$/i", $name)) { $originalFileName = preg_replace("/(?:\\.(?:thumb_thumbnail|thumb_medium|thumb_large)(\\.[^.]+)\$)|(?:\\.thumb)\$/mi", "\$1", $name); if (!file_exists($path . $originalFileName)) { @unlink($path . $name); } } else { $fileName = $name; if (!\FWSystem::detectUtf8($fileName)) { $fileName = utf8_encode($fileName); } $file['icon'][] = self::getFileTypeIconWebPath($path . $name); $file['name'][] = $fileName; $file['size'][] = $this->_getSize($path . $name); $file['type'][] = $this->_getType($path . $name); $file['date'][] = $this->_getDate($path . $name); $file['perm'][] = $this->_getPerm($path . $name); } } } $name = @readdir($fd); } @closedir($fd); clearstatcache(); } $dirTree['dir'] = $dir; $dirTree['file'] = $file; return $dirTree; }