Directory + sub dirs uitlezen probleem
Dit script is onderdeel van een simpele cms pagina die ik gebruik. Nu ben ik er achter gekomen dat er een kleine fout in zit waarvoor ik de oplossing niet gevonden krijg.
Dit script moet de map "images" ($imagedir) + de sub-dirs scannen op images.
Wanneer er images in de hoofd map (images) staan, worden de sub-dirs niet meer uitgelezen.
Wanneer er GEEN images in de hoofd map staan, worden de sub-dirs WEL uitgelezen.
Waar gaat het fout?
Alvast bedankt!
Script:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?
//This gets all files matching $match. It looks recursively using glob or the find command.
function GetMatchingFiles ($matches) {
if (!is_array ($matches)) $matches = array ($matches); //the things to match
$files = array(); //the array of files
foreach ($matches as $match) {
//if the glob function exists
if (function_exists ("glob") && ($globfiles = glob ($match)) && is_array ($globfiles)) $files = array_merge ($files, $globfiles);
//or else use the find function
else if (exec ('find ' . dirname ($match) . ' -type f | grep "' . str_replace ('*', '.*', str_replace ('.', '\.', basename ($match))) . '"', $findfiles) && is_array ($findfiles)) $files = array_merge ($files, $findfiles);
//or else just add the file
else if (!preg_match ('/\*/', $match)) array_push ($files, $match);
}
return $files;
}
//Get the image files from a directory and its subdirectories
function GetImageFiles ($imagedir) {
$imageendings = array ('gif', 'jpg', 'JPG', 'jpeg', 'JPEG', 'png');
$a = array(); foreach ($imageendings as $ending) $a = array_merge ($a, GetMatchingFiles ("$imagedir*.$ending"));
$images = array(); foreach ($a as $image) $images[$image] = preg_replace ("|^$imagedir|", '', $image); //with and without the directory
return $images;
} ?>
//This gets all files matching $match. It looks recursively using glob or the find command.
function GetMatchingFiles ($matches) {
if (!is_array ($matches)) $matches = array ($matches); //the things to match
$files = array(); //the array of files
foreach ($matches as $match) {
//if the glob function exists
if (function_exists ("glob") && ($globfiles = glob ($match)) && is_array ($globfiles)) $files = array_merge ($files, $globfiles);
//or else use the find function
else if (exec ('find ' . dirname ($match) . ' -type f | grep "' . str_replace ('*', '.*', str_replace ('.', '\.', basename ($match))) . '"', $findfiles) && is_array ($findfiles)) $files = array_merge ($files, $findfiles);
//or else just add the file
else if (!preg_match ('/\*/', $match)) array_push ($files, $match);
}
return $files;
}
//Get the image files from a directory and its subdirectories
function GetImageFiles ($imagedir) {
$imageendings = array ('gif', 'jpg', 'JPG', 'jpeg', 'JPEG', 'png');
$a = array(); foreach ($imageendings as $ending) $a = array_merge ($a, GetMatchingFiles ("$imagedir*.$ending"));
$images = array(); foreach ($a as $image) $images[$image] = preg_replace ("|^$imagedir|", '', $image); //with and without the directory
return $images;
} ?>
Gewijzigd op 06/01/2011 18:16:23 door Peps from Yesterday
Er zijn nog geen reacties op dit bericht.