• home
  • forum
  • my
  • kt
  • download
  • Scanning Folders with PHP

    Author: 2009-03-08 20:34:39 From:

    Portfolio Page

    Our Mission

    Let's briefly outline what we need to accomplish.

    • Use PHP to scan our portfolio folder. It will then cyle through each file, that is an image, and then display it on the page.
    • Style our content a bit using CSS.
    • When the users clicks on a thumbnail, we'll use jQuery to load the large version of the image in the main panel.
    • If the user has Javascript disabled, he'll simply be directed to a new page that contains the full-sized image

    How to Use It

    Adding a new image to our portfolio is simple. Take a snapshot of your website, brochure, postcard, etc and size it to 500px x 350px. Place this image within the "images/featured" folder.

    Next, create a thumbnail that is 50px x 50px. Place this image within the "images/tn" folder.

    You must make sure that both the full-sized and the thumbnail images have the exact same file name.

    Our Final HTML

    view plaincopy to clipboardprint?
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">   
    2. <html xmlns="http://www.w3.org/1999/xhtml">   
    3. <head>   
    4.     <title>Scanning Directories with PHP</title>   
    5.     <script src="js/jquery-1.2.6.pack.js" type="text/javascript"></script>   
    6.     <script src="js/scripts.js" type="text/javascript"></script>   
    7.     <link href="default.css" rel="stylesheet" type="text/css" />   
    8. </head>   
    9. <body>   
    10.     <div id="container">   
    11.     <h1>Some Portfolio</h1>   
    12.         <div id="featured">   
    13.             <?php    
    14.                 $featured_dir = 'images/featured/';   
    15.                 $scan = scandir($featured_dir);   
    16.                 echo '<img src="'$featured_dir . $scan[2] . '" alt="image" />';    
    17.             ?>   
    18.         </div>   
    19.            
    20.         <ul id="options">   
    21.         <?php    
    22.             $dir = 'images/tn/';   
    23.             $scan = scandir($dir);   
    24.   
    25.             for ($i=0; $i<count($scan); $i++) {   
    26.              if ($scan[$i] != '.' && $scan[$i] != '..') {   
    27.              echo '  
    28.              <li>  
    29.               <a href="' . $featured_dir . $scan[$i] . '">  
    30.                <img src="'$dir . $scan[$i] . '" alt="'$scan[$i] . '" />  
    31.               </a>  
    32.              </li>';   
    33.              }     
    34.             }   
    35.         ?>   
    36.         </ul>   
    37.     </div>   
    38. </body>   
    39. </html>  

    Our Final CSS

    View it here.

    Compensating For IE6

    IE6 Problem

    Luckily, we only have one thing to fix. If you look at the image above, you'll see that the #options unordered list is not containing its floated list items. While modern browsers will correctly clear the items thanks to our "overflow: hidden;" style, IE6 needs one more rule. Append this to your stylesheet.

    view plaincopy to clipboardprint?
    1. ul#options {   
    2. ...misc styles...   
    3. height: 1%;   
    4. }  

    I could have set the height to anything and it would still work. Think of it as Drago punching IE6 in the face and telling it, "Wake up!". This style forces IE6 to expand as much as is need to clear its children.

    The Complete Javascript(jQuery)

    view plaincopy to clipboardprint?
    1. $(function() {   
    2.     $.preloadImage = function(path) {   
    3.         $("#featured img").attr("src", path);   
    4.     }   
    5.   
    6.     $('ul#options li img').click(function() {   
    7.         $('ul li img').removeClass('selected');                        
    8.         $(this).addClass('selected');                                  
    9.   
    10.         var imageName = $(this).attr('alt');                           
    11.   
    12.        $.preloadImage('images/featured/' + imageName);   
    13.   
    14.         var chopped = imageName.split('.');                            
    15.         $('#featured h2').remove();                                    
    16.         $('#featured')                                                 
    17.          .prepend('<H2 class=description>' + chopped[0] + '</H2>').children('h2').fadeIn(500).fadeTo(200, .6);   
    18.     });   
    19.   
    20.     $('ul#options li a').click(function() {                            
    21.         return false;   
    22.     });   
    23. });  

    discuss this topic to forum

    relation tutorial

    No information

    Category

      Ad Management (6)
      Calendars (3)
      Chat Systems (8)
      Content Management (41)
      Cookies and Sessions (12)
      Counters (15)
      Database Related (34)
      Date and Time (15)
      Development (22)
      Discussion Boards (8)
      E Commerce (8)
      Email Systems (14)
      Error Handling (8)
      File Manipulation (33)
      Flash and PHP (6)
      Form Processing (19)
      Guestbooks (12)
      Image Manipulation (21)
      Installing PHP (7)
      Introduction to PHP (23)
      Link Indexing (8)
      Mailing List Management (9)
      Miscellaneous (53)
      Networking (8)
      News Publishing (9)
      OOP (24)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (6)
      Postcards (1)
      Randomizing (14)
      Redirection (11)
      Searching (9)
      Security (29)
      Site Navigation (16)
      User Authentication (14)
      WAP and WML (7)
      Web Fetching (8)
      Web Traffic Analysis (15)
      XML and PHP (16)

    New

    Hot