Hi, I am very new to php and am playing with a little bit of code to display some thumbnail images on an html page.

I am confused because I can get the code to work if its placed in the image directory and called from there, but not if I try to reference the code from the root directory. The problem lies in the first 'IF' statement of the function called 'displayphotos' - line 3.

the code that works reads (the code that doesn't is below that:

<?php

$columns = 4;
$thmb_width = 118;
$thmb_height = 118

function getNormalImage($file){
$base = substr($file,0,strrpos($file,'_th.jpg'));
if (file_exists($base.'.jpg')) return $base.'.jpg';
elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';
else return "";
}

function displayPhotos(){
global $columns;
$act = 0;
if ($handle = opendir(".")) { // THIS IS THE OFFENDING VALUE
// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether the actual item is a valid file
if (is_file($file)){
// Check whether the actual image is a thumbnail
if (strpos($file,'_th.jpg')){
++$act;
if ($act > $columns) {
echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/>[/url]</td>';

$act = 1;
} else {
echo '<td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/>[/url]</td>';

}

           }
        }
  }

}

}

?>

and the code that dosen't work reads:

<?php

define(GALLERY_ROOT, "./portfolio/2ded/");
$columns = 4;
$thmb_width = 118;
$thmb_height = 118;

function getNormalImage($file){
$base = substr($file,0,strrpos($file,'_th.jpg'));
if (file_exists($base.'.jpg')) return $base.'.jpg';
elseif (file_exists($base.'.jpeg')) return $base.'.jpeg';
else return "";
}

function displayPhotos(){
global $columns;

$act = 0;
// Open the actual directory
if ($handle = opendir(GALLERY_ROOT)) {
// Read all file from the actual directory
while ($file = readdir($handle)) {
// Check whether the actual item is a valid file
if (is_file($file)){
// Check whether the actual image is a thumbnail
if (strpos($file,'_th.jpg')){
++$act;
if ($act > $columns) {
echo '</tr><tr><td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/>[/url]</td>';

$act = 1;
} else {
echo '<td class="photo"><a href="'.getNormalImage($file).'"><img src="'.$file.'" alt="'.$file.'"/>[/url]</td>';

}

           }
        }
  }

}

}

?>

Any help would be great! Thanks in anticipation.

xymbo

    Welcome to the forums. In the future, you can make it easier on everyone who want to help by placing your code snippets within

    [/b] [url=http://phpbuilder.com/board/misc.php?do=bbcode]bbcode tags[/url].
    
    The problem may be that you are using a relative path from the current directory in this line:
    [code=php]
    define(GALLERY_ROOT, "./portfolio/2ded/");
    

    You might want to turn it into an absolute path with something like:

    define(GALLERY_ROOT, $_SERVER['DOCUMENT_ROOT'] . "/portfolio/2ded/");
    
      Write a Reply...