Web Programming Languages

PHP Source Code: images.php

<?php
//----------------------------------------------------------
//   The Web Language Project
//   Mark Brautigam
//   May-June 2015
//   http://www.mixed-up.com/markb/
//----------------------------------------------------------

include "common.php";
headers1();
headers2();
echo sidebar("php", "images", "php");
?>

  <div id="content">
    <h2>PHP: Directory Read</h2>
    <p>Read the contents of a directory of images. Exclude the non-image directory entries.
       Create a caption by deleting the file extension and capitalizing the file name.</p>
    <p class='choose'>Choose a directory:
      <a href='images.php?dir=comics'>Comics</a> • 
      <a href='images.php?dir=embroidery'>Embroidery</a> • 
      <a href='images.php?dir=stone'>Carving</a> • 
      <a href='images.php?dir=tech'>Tech</a>
    </p>
    <?php
      //-------------------------------------------------------------
      //  The main PHP code is here. 
      //-------------------------------------------------------------
      $dir = "../images/stone";
      if (isset ($_GET['dir']) && $_GET['dir'] != "")
        $dir = "../images/" . $_GET['dir'];

      $images = scandir ($dir);
      $nimages = count ($images);
      for ($i=0; $i<$nimages; $i++) {
        if ($images[$i][0] != '.' && $images[$i][0] != '_') {
          $caption = ucfirst (substr ($images[$i], 0, -4));

          if (strchr ($caption, "_") !== false) {
            $captions = explode ("_", $caption);
            foreach ($captions as $k => $v)
              $captions[$k] = ucfirst ($captions[$k]);
            $caption = join ("<br />", array_slice ($captions, 1));
          }

          echo "<figure>\n";
          echo "  <img src='$dir/{$images[$i]}' />\n";
          echo "  <figcaption>$caption</figcaption>\n";
          echo "</figure>\n";
        }
      }
      //-------------------------------------------------------------
    ?>
    <p>We can change the order of the files (for example, birth order instead of alphabetical
       order) by prepending each file name with a number and removing the number from the
       caption string.</p>
    <p>Images courtesy of:<br />
    <a target='_blank'
       href='http://www.worldofjudaica.com/jewish-home/stationery/p_jerusalem_stone_paperweight_with_twelve_tribes_and_menorah'>
       Stone Paperweight at World of Judaica</a><br />
     <a target='_blank'
        href='http://4-hobby.com/store/product.php?productid=17686&cat=320&page=1'>
        Embroidery Designs at 4-Hobby.com</a><br />
     <a target='_blank' href='http://www.dumbingofage.com'>Dumbing of Age</a><br />
     <a target='_blank' href='http://www.scarygoround.com'>Scary Go Round</a><br />
     <a target='_blank' href='http://www.questionablecontent.net'>Questionable Content</a>
    </p>
    <p><a href='source.php?f=5'>Show PHP source code »</a>
    </p>
  </div>

<?php 
footers(); 
?>