galImageSource

default
galImage

I built this code for a project I was working on. When I wrote it I was not considering that it was going to be viewed by others I don't have the time to comment and clean it up right now. I'm putting it up regardless. How useful is a PHP image gallery without the code?

Here is the code for listDir.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

$thisDir = getcwd().'/listDir.php';
$titleOriginal = basename(dirname($thisDir));
#$title = ereg_replace('_', ' ', $titleOriginal);
# ereg has been deprecated
$title = preg_replace('/_/', ' ', $titleOriginal);
$logo = '<b>galImage</b>';
?>
<title><?php print $title; ?></title>
</head>

<body>
<center>


<?php
if(!isSet($_GET['start'])){
	$_GET['start'] = 0;
}
#if(($_GET['start']) == 0) {
#  $_GET['start'] = 1;
#}
$prevStart = $_GET['start'] - 15;
$nextStart = $_GET['start'] + 15;
?>

<?php
//display pics in a directory called images
//-----------------------------------------
$cont=0;
$tr=1;
$string="";
$imgDir='thumbs/';
if ($dir = opendir("thumbs")) {
  while (($file = readdir($dir)) !== false) {
    if ($file == "." || $file == ".." || $file == false) { } else {
    $cont++;
      $string[$cont]=($file);
    }
  }
  closedir($dir);
}

sort($string);
#print_r ($string);
?>

<table border="0">
  <tr><td colspan="5" align="center"><?php print $logo;?></td></tr>
  <tr valign="middle"><td colspan="5" align="center"><h1><?php print $title;?></h1></td></tr>
<?php
$numRow = 3;
#$imgPerPage = 5;
$i = $_GET['start'];

for ($row=0; $row < 3; $row++)
  {
  print("  <tr>\n");
  for ($col=0; $col < 5; $col++)
    {
    if($i < count($string))
      {
      print("    <td align=\"center\"><a href=\"soloImg.php?i=$i\"><img src=\"$imgDir$string[$i]\" border=\"0\"></a></td>\n");
      }
      else
      {
      print("    <td> </td>\n");
      }
    $i++;
    }
  print("  </tr>\n");
  }

?>


<?php
$homeBtn='<a href="../index.php">home</a>';
$imgTotal = count($string);
//navigation
if(($_GET['start'] == 0) && ($nextStart < $imgTotal)){

//beginning of gallery
?>
  <tr>
    <td colspan="5">
      <table width="100%" border="0">
        <tr>
          <td width="33%" align="center"> </td>
          <td width="34%" align="center"><?php print $homeBtn;?></td>
          <td width="33%" align="center"><a href="listDir.php?start=<?php print($nextStart);?>">next</a></td>
        </tr>
      </table>
    </td>
  </tr>
<?php
}
elseif(($_GET['start'] > 1) && ($nextStart < $imgTotal)){
//middle of gallery
?>
  <tr>
    <td colspan="5">
      <table width="100%" border="0">
        <tr>
          <td width="33%" align="center"><a href="listDir.php?start=<?php print($prevStart);?>">previous</a></td>
          <td width="34%" align="center"><a href="../index.php">home</a></td>
          <td width="33%" align="center"><a href="listDir.php?start=<?php print($nextStart);?>">next</a></td>
        </tr>
      </table>
    </td>
  </tr>
<?php
}
elseif(($_GET['start'] > 1) && ($nextStart >= $imgTotal)){
//end of gallery
?>
  <tr>
    <td colspan="5">
      <table width="100%" border="0">
        <tr>
          <td width="33%" align="center"><a href="listDir.php?start=<?php print($prevStart);?>">previous</a></td>
          <td width="34%" align="center"><a href="../index.php">home</a></td>
          <td width="33%" align="center"> </td>
        </tr>
      </table>
    </td>
  </tr>
<?php
}

else {
//one page gallery
?>
  <tr>
    <td colspan="5">
      <table width="100%" border="0">
        <tr>
          <td width="33%" align="center"> </td>
          <td width="34%" align="center"><a href="index.php">home</a></td>
          <td width="33%" align="center"> </td>
        </tr>
      </table>
    </td>
  </tr>
<?php
}
?>


</table>

</center>
</body>
</html>



Here is the code for soloImg.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<center>


<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

//display pics in a directory called images
//-----------------------------------------
$cont=0;
$i= $_GET['i'];
$string="";
$imgDir = 'images/';
if ($dir = opendir("images")) {
  while (($file = readdir($dir)) !== false) {
    if ($file == "." || $file == ".." || $file == false) { } else {
    $cont++;
      $string[$cont]=($file);
    }
  }
  closedir($dir);
}

sort($string);
?>

<?php
print '<img src="'.$imgDir.$string[$i].'">';
?>

<br>

<?php
$prevImg = $i - 1;
$nextImg = $i + 1;
$imgTotal = count($string);
$rndUpStart = floor($i / 15)*15;

//navigation
if(($i == 0) && ($nextImg < $imgTotal)){
//beginning of images
?>
<a href="listDir.php?start=<?php print $rndUpStart; ?>">gallery</a>
<a href="soloImg.php?i=<?php print($nextImg);?>">next</a>
<?php
}
elseif(($i > 0) && ($nextImg < $imgTotal)){
//middle of images
?>
<a href="soloImg.php?i=<?php print($prevImg);?>">previous</a>
<a href="listDir.php?start=<?php print $rndUpStart;?>">gallery</a>
<a href="soloImg.php?i=<?php print($nextImg);?>">next</a>
<?php
}
elseif(($i > 0) && ($nextImg >= $imgTotal)){
//end of images
?>
<a href="soloImg.php?i=<?php print($prevImg);?>">previous</a>
<a href="listDir.php?start=<?php print $rndUpStart;?>">gallery</a>
<?php
}
else{
//only one image
?>
<a href="listDir.php?start=<?php print $rndUpStart;?>">gallery</a>
<?php
}
?>
</center>
</body>
</html>

galImage
default