PHP Script to Load images from a Folder but Excluding some images using Wildcard

johnnygaddaar.cs58

New Member
Messages
21
Reaction score
1
Points
0
Hello,

I am using TILTVIEWER in my Website for displaying my Photo Gallery Images in a Flash View here http://www.clan-icsl.com/images/photoalbum/

Actually my Photo Gallery stores all Thumbnail and Enlarged images in a single folder. And I want to display only the enlarged images.

So using the code given below, I am able to GET all the images.

But, I am unable to exclude particular images, i.e, Thumb images as *_t1.jpg and *_t2.jpg where t1 and t2 refers to Thumbnail 1 and 2 of the Enlarged images.

So please help me to edit this code to get my problem resolved.

<?php
echo "<tiltviewergallery>
<photos>";
$files = glob("*/*.jpg");
for ($i=1; $i<count($files); $i++)
{
$num = $files[$i];
echo '<photo imageurl="'.$num.'" linkurl="http://www.google.com">
<title>Image 1</title>
<description>This is a regular text description.</description>
</photo>';
}
echo '</photos>
</tiltviewergallery>';
?>
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Note: you can use
PHP:
, [html] or [code] tags (as appropriate) to preserve formatting and (in some cases) get colorized code.

[FONT="Courier New"]glob[/FONT] patterns don't offer a way of excluding files, but if the files you want to include , you might be able to use a more specific pattern (e.g. only the files you want end with '_f.jpg', so you use a pattern of '*_f.jpg'). You can filter out the entries using [URL="http://php.net/array_filter"][FONT="Courier New"]array_filter[/FONT][/URL], [URL="http://php.net/preg_grep"][FONT="Courier New"]preg_grep[/FONT][/URL], or within your loop:

[php]<?php
// <= PHP 5.2
$files = array_filter(glob("*/*.jpg"), 
    create_function('$name', 'return ! preg_match("/_t[12]\.jpg$/", $name);'));
// >= PHP 5.3
$files = array_filter(glob("*/*.jpg"), 
    function($name) { return ! preg_match('/_t[12]\.jpg$/', $name);});

// or
$files = preg_grep('/_t[12]\.jpg$/', glob("*/*.jpg"), PREG_GREP_INVERT);

// or
foreach (glob("*/*.jpg") as $file) {
  if (! preg_match('/_t[12]\.jpg$/', $file)) {
    ?>
    <photo imageurl="<?php echo $file; ?>" linkurl="http://www.google.com">
      <title>Image 1</title>
      <description>This is a regular text description.</description>
    </photo>
    <?php
  }
}

Another, simpler approach is to separate thumbnails from full images, storing each in separate directories. If you create a "thumbs" subfolder in each image folder, you wouldn't even have to change your glob pattern.
 
Last edited:
Top