• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
unwanted item in image gallery

#1
[eluser]learning_php[/eluser]
Hi,

I have written a basic image gallery but it loads a thumbs.db file which is located in the thumbs folder. How do I define which types of file to display?

Code:
<?php

$images = "thumbs/"; # Location of small versions
$big = "uploads/";
$cols   = 5; # Number of columns to display

if ($handle = opendir($images)) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." ) {
           $files[] = $file;
       }
   }
   closedir($handle);
}

$colCtr = 0;

echo '<table width="100%" cellspacing="0"><tr>';

foreach($files as $file)
{
  if($colCtr %$cols == 0)
    echo '</tr><tr><td colspan="5"><hr /></td></tr><tr>';
  echo '<td align="center"><a href="'. $big . $file . '"><img src="' . $images . $file . '" /></a></td>';
  $colCtr++;
}

echo '</table>' . "\r\n";

?&gt;

#2
[eluser]Yorick Peterse[/eluser]
This is a function I used for a simple image gallery, it's quite rough but it does what you are asking:

Code:
//Function to show a single category
    function Category() {
        $URL_Title     = $_GET['title'];        
        
        if($URL_Title !== null) {
            //Check if the category folders that belong to the current title exist and fetch their contents
            $Full_Dir         = "gallery/normal/$URL_Title/";
            $Thumb_Dir        = "gallery/thumbs/$URL_Title/";
            //Scan directories
            if(file_exists($Full_Dir)) {
                $Scan_Full        = scandir($Full_Dir);
            }
            if(file_exists($Thumb_Dir)) {
                $Scan_Thumb        = scandir($Thumb_Dir);
            }
            echo "<div id=\"image-wrap\">";
            for($i = 0; $i<count($Scan_Thumb); $i++) {
                if($Scan_Thumb[$i] != "." && $Scan_Thumb[$i] != ".." && preg_match('/[.](jpg)|(gif)|(png)$/',$Scan_Thumb[$i])) {
                    $FullName = str_replace("thumb_","",$Scan_Thumb[$i]);
                    $ThumbLayout = <<<THUMBS
                    <div class="image-box">
                        <a href="$Full_Dir$FullName" title="$FullName" rel="lightbox[roadtrip]"><img src="$Thumb_Dir$Scan_Thumb[$i]" alt="$Scan_Thumb[$i]" /></a>
                    </div>
THUMBS;
                    echo $ThumbLayout;
                }                
            }
            echo "</div>";
        }
        else {
            echo "<h2>The page you requested does not exist!</h2>";
        }
        echo "</div>";
    }


The most important part is the following:


Code:
if($Scan_Thumb[$i] != "." && $Scan_Thumb[$i] != ".." && preg_match('/[.](jpg)|(gif)|(png)$/',$Scan_Thumb[$i])) {


That piece of code ignores any folders that might have been placed in the folder you wish to scan, it will also ignore everything that does not end with .jpg, .gif or .png


p.s. For all the MVC die-hards, I wrote this function before I learned about seperating logic from presentation Tongue

#3
[eluser]learning_php[/eluser]
Hi,

Thanks for that is changed the line to this
Code:
if ($file != "." && $file != ".." && preg_match('/[.](jpg)|(gif)|(png)$/',$file))
and it works

#4
[eluser]Yorick Peterse[/eluser]
Glad to hear it helped Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.