/** * This function will get a file list using a pattern. You can compare this function with the dir command from * DOS or the ls command from Unix. The pattern syntax is the same as well. * * @remarks * This will not work recursively on the subdirectories. * * @param $pattern (optional) Pattern to which the files should match. If you want multiple items, you can also * pass them as an array. If the pattern is prefixed with an exclamation mark, the files that * match this pattern will not be included in the result. * @param $class (optional) If you specify a not null value for this option, this function will return the * items in the directory as the indicated class. If an empty string is given, it will return * the list of filenames instead of objects. * @param $classes (optional) An array with the classes to include. Standard, it includes YDFSImage, YDFSFile * and YDFSDirectory classes. If you only need a single class, you can also specify it as a * string. * @param $sort_by_date (optional) Sorts the items by date. Default is false. * @param $sort_order (optional) Whether the sort direction is ascending or descending. Default is "ASC". * * @returns Array of YDFile objects for the files that match the pattern. */ function getContents($pattern = '', $class = null, $classes = array('YDFSFile', 'YDFSImage', 'YDFSDirectory'), $sort_by_date = false, $sort_order = 'asc') { // Start with an empty list $fileList = array(); // Get a handle to the directory $dirHandle = opendir($this->getPath()); // Get the list of files while (false !== ($file = readdir($dirHandle))) { if ($file != '.' && $file != '..') { $fileList[strtolower($file)] = $file; } } // Get the list of patterns if (!is_array($pattern)) { $pattern = array($pattern); } // Apply the pattern to match $fileListMatch = array(); foreach ($fileList as $file) { foreach ($pattern as $patternitem) { if (!empty($patternitem) && substr($patternitem, 0, 1) != '!') { if (YDFSDirectory::_match($patternitem, $file)) { $fileListMatch[$file] = $file; } } else { $fileListMatch[$file] = $file; } } } $fileList =& $fileListMatch; // Apply the patterns to not match foreach ($fileList as $file) { foreach ($pattern as $patternitem) { if (!empty($patternitem) && substr($patternitem, 0, 1) == '!') { if (YDFSDirectory::_match(substr($patternitem, 1), $file)) { unset($fileList[$file]); } } } } // Get the values $fileList = array_values($fileList); // Convert the list of a list of YDFile objects $fileList2 = array(); foreach ($fileList as $file) { $file = $this->getPath() . '/' . $file; if (!is_null($class) && $class != '') { $fileObj = new $class($file); } else { if (is_dir($file)) { $fileObj = new YDFSDirectory($file); } else { if (YDPath::isImage($file)) { $fileObj = new YDFSImage($file); } else { $fileObj = new YDFSFile($file); } } } if ($sort_by_date === true) { $fileList2[filectime($file) . strtolower($file)] = $fileObj; } else { $fileList2[strtolower($file)] = $fileObj; } } // Sort the list of files if (strtolower($sort_order) != 'desc') { ksort($fileList2); } else { krsort($fileList2); } $fileList2 = array_values($fileList2); // Remove the unsupported classes if (!is_array($classes)) { $classes = array($classes); } if (sizeof($classes) == 0) { return array(); } foreach ($classes as $key => $val) { $classes[$key] = strtolower($val); } foreach ($fileList2 as $key => $val) { if (!in_array(strtolower(get_class($val)), $classes)) { unset($fileList2[$key]); } } // Return a simple list if needed if ($class === '') { // Initialize a list for the files only $fileOnlyList = array(); // Add the files foreach ($fileList2 as $file) { array_push($fileOnlyList, basename($file->_path)); } // Return the fileOnlyList array return $fileOnlyList; } // Return the file list return $fileList2; }
/** * This function will get a file list using a pattern. You can compare this function with the dir command from * DOS or the ls command from Unix. The pattern syntax is the same as well. * * @remarks * This will not work recursively on the subdirectories. * * @param $pattern (optional) Pattern to which the files should match. If you want multiple items, you can also * pass them as an array. If the pattern is prefixed with an exclamation mark, the files that * match this pattern will not be included in the result. * @param $class (optional) If you specify a not null value for this option, this function will return the * items in the directory as the indicated class. If an empty string is given, it will return * the list of filenames instead of objects. * @param $classes (optional) An array with the classes to include. Standard, it includes YDFSImage, YDFSFile * and YDFSDirectory classes. If you only need a single class, you can also specify it as a * string. * @param $sort_by_date (optional) Sorts the items by date. Default is false. * @param $sort_order (optional) Whether the sort direction is ascending or descending. Default is "ASC". * * @returns Array of YDFile objects for the files that match the pattern. */ function getContents($pattern = '', $class = null, $classes = array('YDFSFile', 'YDFSImage', 'YDFSDirectory'), $sort_by_date = false, $sort_order = 'asc') { // Start with an empty list $fileList = array(); $fileListMatch = array(); // Get the list of patterns if (!is_array($pattern)) { $pattern = array($pattern); } // Check if there other patterns than exceptions $hasMoreThanExceptions = false; foreach ($pattern as $patternitem) { if (!empty($patternitem) && substr($patternitem, 0, 1) != '!') { $hasMoreThanExceptions = true; break; } } // Get the files that match if ($hasMoreThanExceptions == true) { foreach ($pattern as $patternitem) { if (!empty($patternitem) && substr($patternitem, 0, 1) != '!') { $glob_files = glob($this->getAbsolutePath() . '/' . $patternitem); if ($glob_files !== false) { foreach ($glob_files as $file) { $file = basename($file); $fileListMatch[$file] = $file; } } } else { if (empty($patternitem)) { $glob_files = glob($this->getAbsolutePath() . '/*'); if ($glob_files !== false) { foreach (glob($this->getAbsolutePath() . '/*') as $file) { $file = basename($file); $fileListMatch[$file] = $file; } } } } } } else { $glob_files = glob($this->getAbsolutePath() . '/' . '*'); if ($glob_files !== false) { foreach ($glob_files as $file) { $file = basename($file); $fileListMatch[$file] = $file; } } } $fileList =& $fileListMatch; // Remove the files that don't match foreach ($pattern as $patternitem) { if (!empty($patternitem) && substr($patternitem, 0, 1) == '!') { $glob_files = glob($this->getAbsolutePath() . '/' . substr($patternitem, 1)); if ($glob_files !== false) { foreach ($glob_files as $file) { $file = basename($file); unset($fileList[$file]); } } } } // Get the values $fileList = array_values($fileList); // Convert the list of a list of YDFile objects $fileList2 = array(); foreach ($fileList as $file) { $file = $this->getPath() . '/' . $file; if (!is_null($class) && $class != '') { $fileObj = new $class($file); } else { if (is_dir($file)) { $fileObj = new YDFSDirectory($file); } else { if (YDPath::isImage($file)) { $fileObj = new YDFSImage($file); } else { $fileObj = new YDFSFile($file); } } } if ($sort_by_date === true) { $fileList2[filectime($file) . strtolower($file)] = $fileObj; } else { $fileList2[strtolower($file)] = $fileObj; } } // Sort the list of files if (strtolower($sort_order) != 'desc') { ksort($fileList2); } else { krsort($fileList2); } $fileList2 = array_values($fileList2); // Remove the unsupported classes if (!is_array($classes)) { $classes = array($classes); } if (sizeof($classes) == 0) { return array(); } foreach ($classes as $key => $val) { $classes[$key] = strtolower($val); } foreach ($fileList2 as $key => $val) { if (!in_array(strtolower(get_class($val)), $classes)) { unset($fileList2[$key]); } } // Return a simple list if needed if ($class === '') { // Initialize a list for the files only $fileOnlyList = array(); // Add the files foreach ($fileList2 as $file) { array_push($fileOnlyList, basename($file->_path)); } // Return the fileOnlyList array return $fileOnlyList; } // Return the file list return $fileList2; }