file extention - wildcard
file extention - wildcard
am 21.09.2007 21:57:05 von Hans-Peter Sauer
I have the following list of image files .
When searching the latest (numbered file) in this particular case its
background_4.*** and its a .jpg file - but - the latest file in the list
could also be a .gif file at times .
zimages_background/background_1.jpg
zimages_background/background_2.jpg
zimages_background/background_3.gif
zimages_background/background_4.jpg
etc etc
How do i do a wildcard file extention search to find the latest numbered
image - regardless if its a .jpg or .gif .
I know how to search for the _* number in the filename & its how to do a
file extention wildcard search i need .
--
(stuckle and his lapdogs are forbidden from replying to this thread as i
dont actually want any help from them)
Re: file extention - wildcard
am 21.09.2007 23:44:35 von Steve
"Krustov" wrote in message
news:MPG.215e325d9ecaa50e98ad25@news.newsreader.com...
>I have the following list of image files .
>
> When searching the latest (numbered file) in this particular case its
> background_4.*** and its a .jpg file - but - the latest file in the list
> could also be a .gif file at times .
>
> zimages_background/background_1.jpg
> zimages_background/background_2.jpg
> zimages_background/background_3.gif
> zimages_background/background_4.jpg
>
> etc etc
>
> How do i do a wildcard file extention search to find the latest numbered
> image - regardless if its a .jpg or .gif .
>
> I know how to search for the _* number in the filename & its how to do a
> file extention wildcard search i need .
>
>
> --
> (stuckle and his lapdogs are forbidden from replying to this thread as i
> dont actually want any help from them)
hope i'm not one of them...not sure of my association with the lapdogs. if
you mean republican, i tend to go that way on fiscal and domestic policy,
however r's look more d than d's do these days. if you only mean mentally
entrenched to blindly follow...i can assure you i'm not. ;^)
here's a flexible function to list files and one to return the file number,
followed by an example with your data above. if the image doesn't meet
image_number.ext, then the whole file name is returned and the version
number will be zero.
hth,
me
========================
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 '============================
';
echo 'path :: ' . $path . '
';
echo 'file :: ' . $name . '
';
echo 'version :: ' . $version . '
';
}
}
?>
Re: file extention - wildcard
am 22.09.2007 00:05:25 von Hans-Peter Sauer
> > (stuckle and his lapdogs are forbidden from replying to this thread as i
> > dont actually want any help from them)
>
> hope i'm not one of them
Nah - when the xargs take over planet earth - your name will be on the
protected list :-)
>
> 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 '============================
';
> echo 'path :: ' . $path . '
';
> echo 'file :: ' . $name . '
';
> echo 'version :: ' . $version . '
';
> }
> }
> ?>
I did it using the following , But there must be a better and/or easier
way of doing it .
$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
Re: file extention - wildcard
am 22.09.2007 02:25:01 von Steve
>>
>> 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 '
============================
';
>> echo 'path :: ' . $path . '
';
>> echo 'file :: ' . $name . '
';
>> echo 'version :: ' . $version . '
';
>> }
>> }
>> ?>
>
> 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.
>
>
>
> $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
Re: file extention - wildcard
am 22.09.2007 16:19:01 von Hans-Peter Sauer
> > 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.
>
www.vhit.co.uk/images/steve.jpg
Its nowt important as its only a guestbook i'm writing .
There was no way for you to know this - but a user would need to ftp and
upload into the correct folder to screw things up .
I figure most newbie users who install and use this sort of thing will
only ever use the web interface .
--
(c) The Amazing Krustov
Re: file extention - wildcard
am 22.09.2007 20:32:47 von Steve
"Krustov" wrote in message
news:MPG.215f3439616da80c98ad29@news.newsreader.com...
>
>
>
>
>
>> > 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.
>>
>
> www.vhit.co.uk/images/steve.jpg
>
> Its nowt important as its only a guestbook i'm writing .
>
> There was no way for you to know this - but a user would need to ftp and
> upload into the correct folder to screw things up .
>
> I figure most newbie users who install and use this sort of thing will
> only ever use the web interface .
probably true. looks good.
best to you,
me