|
Posted by Steve on 09/22/07 00:25
>> <?
>> function listFiles($path = '.', $extension = array(), $combine = false)
>> {
>> $wd = getcwd();
>> $path .= substr($path, -1) != '/' ? '/' : '';
>> if (!chdir($path)){ return array(); }
>> if (!$extension){ $extension = array('*'); }
>> if (!is_array($extension)){ $extension = array($extension); }
>> $extensions = '*.{' . implode(',', $extension) . '}';
>> $files = glob($extensions, GLOB_BRACE);
>> chdir($wd);
>> if (!$files){ return array(); }
>> $list = array();
>> foreach ($files as $file)
>> {
>> $list[] = ($combine ? $path : '') . $file;
>> }
>> return $list;
>> }
>> function getImages($value, $key, &$images)
>> {
>> $pathInfo = pathinfo($value);
>> $path = $pathInfo['dirname'];
>> $fileName = $pathInfo['basename'];
>> $fileNumber = 0;
>> $matches = array();
>> if (preg_match("/^([^_]*?)_(\d+).*$/", $fileName, $matches))
>> {
>> $fileName = $matches[1];
>> $fileNumber = max($images[1][$path][$fileName], $matches[2]);
>> }
>> $images[1][$path][$fileName] = $fileNumber;
>> }
>> $files = listFiles('zimages_background', null, true);
>> $images = array();
>> $imageList = array('', &$images);
>> array_walk($files, 'getImages', $imageList);
>> foreach ($images as $path => $files)
>> {
>> foreach ($files as $name => $version)
>> {
>> echo '<pre>============================</pre>';
>> echo '<pre>path :: ' . $path . '</pre>';
>> echo '<pre>file :: ' . $name . '</pre>';
>> echo '<pre>version :: ' . $version . '</pre>';
>> }
>> }
>> ?>
>
> I did it using the following , But there must be a better and/or easier
> way of doing it .
not really. if the below works for you, that's what you need to use i
suppose. what i did above is a bit more lenient. it doesn't care what the
file name is or in what directory you are looking or what extention a file
has (it automatically screens out other images like .tif, etc.). i usually
try to build around the concept of what i'm going for rather than
pigeon-hole-ing myself into maintaining code - say, if i started using other
types of images in this case.
like i said. if the below works, go for it.
cheers.
>
> <?php
>
> $batman=1;
>
> $dir="zimages_background/";
> $joker=opendir($dir);
> while (false!==($boywonder=readdir($joker)))
> {
> $ripper=substr($boywonder,0,11);
> $pizza=1;
> if ($boywonder==".") {$pizza=0;}
> if ($boywonder=="..") {$pizza=0;}
> if ($ripper<>"background_") {$pizza=0;}
> if ($pizza==1) {$files[$batman]=$boywonder; $batman=$batman+1;}
> }
> closedir($joker);
>
> $joker=1;
>
> while ($joker<$batman)
> {
> $temp1=($files[$joker]);
> $temp2=strlen($temp1);
> $temp3=substr($temp1,$temp2-4,4);
> $temp4=str_replace("background_","",$temp1);
> $temp4=str_replace(".jpg","",$temp4);
> $temp4=str_replace(".png","",$temp4);
> $temp4=str_replace(".gif","",$temp4);
> $cake[$temp4]=$temp3;
> $joker=$joker+1;
> }
>
> $batman=$batman-1;
>
> ?>
>
>
> --
> (c) The Amazing Krustov
Navigation:
[Reply to this message]
|