Add recursive glob for epub directory

This commit is contained in:
Marsender 2013-10-07 23:00:06 +01:00
parent 89a27f10b8
commit df87add1b0
3 changed files with 41 additions and 3 deletions

View file

@ -14,7 +14,7 @@ try {
echo sprintf('Export ebooks to %s', $fileName) . '<br />'; echo sprintf('Export ebooks to %s', $fileName) . '<br />';
// Add the epub files into the export file // Add the epub files into the export file
if (!empty($dbConfig['epub_path'])) { if (!empty($dbConfig['epub_path'])) {
$fileList = glob($dbConfig['epub_path'] . DIRECTORY_SEPARATOR . '*.epub'); $fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub');
foreach ($fileList as $fileName) { foreach ($fileList as $fileName) {
$export->AddEpub($fileName); $export->AddEpub($fileName);
} }

View file

@ -14,7 +14,7 @@ try {
echo sprintf('Load database %s', $fileName) . '<br />'; echo sprintf('Load database %s', $fileName) . '<br />';
// Add the epub files into the database // Add the epub files into the database
if (!empty($dbConfig['epub_path'])) { if (!empty($dbConfig['epub_path'])) {
$fileList = glob($dbConfig['epub_path'] . DIRECTORY_SEPARATOR . '*.epub'); $fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub');
foreach ($fileList as $fileName) { foreach ($fileList as $fileName) {
$db->AddEpub($fileName); $db->AddEpub($fileName);
} }

View file

@ -62,6 +62,44 @@ $dbNum = isset($_GET['dbnum']) ? (int)$_GET['dbnum'] : null;
// Include html header // Include html header
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'header.php'); require_once(__DIR__ . DIRECTORY_SEPARATOR . 'header.php');
/**
* Recursive get files
*
* @param string Base directory to search in
* @param string Search pattern
*/
function RecursiveGlob($inPath = '', $inPattern = '*')
{
$res = array();
// Check path
if (!is_dir($inPath)) {
return $res;
}
// Get the list of directories
if (substr($inPath, -1) != DIRECTORY_SEPARATOR) {
$inPath .= DIRECTORY_SEPARATOR;
}
// Add files from the current directory
$files = glob($inPath . $inPattern, GLOB_MARK | GLOB_NOSORT);
foreach ($files as $item) {
if (substr($item, -1) == DIRECTORY_SEPARATOR) {
continue;
}
$res[] = $item;
}
// Scan sub directories
$paths = glob($inPath . '*', GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT);
foreach ($paths as $path) {
$res = array_merge($res, RecursiveGlob($path, $inPattern));
}
return $res;
}
// Html content // Html content
if (isset($action) && isset($dbNum)) { if (isset($action) && isset($dbNum)) {
if (!isset($gConfig['databases'][$dbNum])) { if (!isset($gConfig['databases'][$dbNum])) {
@ -102,7 +140,7 @@ else {
$str .= '</tr>' . "\n"; $str .= '</tr>' . "\n";
$actionTitle = $gConfig['actions'][$action]; $actionTitle = $gConfig['actions'][$action];
foreach ($gConfig['databases'] as $dbNum => $dbConfig) { foreach ($gConfig['databases'] as $dbNum => $dbConfig) {
$fileList = glob($dbConfig['epub_path'] . DIRECTORY_SEPARATOR . '*.epub'); $fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub');
$str .= '<tr>' . "\n"; $str .= '<tr>' . "\n";
$str .= '<td>' . $dbNum . '</td>' . "\n"; $str .= '<td>' . $dbNum . '</td>' . "\n";
$str .= '<td>' . $dbConfig['name'] . '</td>' . "\n"; $str .= '<td>' . $dbConfig['name'] . '</td>' . "\n";