1

I have made a php image gallery, which should list all the subdirectories of the of the "pics" folder and when clicked, show the first image in the folder with a link to the previous and next photos. When it lists the subdirectories of the "pics" folder on line 20, nothing is returned. Also, the next and previous links always show the links to the Albums page instead of the next image.

What have I done wrong? Any critiques of my code would be appreciated as well.

<?
//Return the contents of a folder which are images as an array
function dirContents($folder){
if ($handle = opendir($folder)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != ".." && !is_dir($file) && (pathinfo($file,PATHINFO_EXTENSION) == 'jpg')) {
            $contents[] = $file;
            echo "$file</br>";
        }
    }
    closedir($handle);
}   
    return $contents;
}

if (!isset($_GET['album'])){
    //List all the albums from the pics folder
    echo '<div class="subhead">Albums</div>';
    echo '<ul>';
    if ($handle = opendir("./pics")) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && is_dir($file)) {
                echo '<li><a href="?page=gallery&album='.$file.'&i=0">'. $file. '</a></li>';
            }
        }
        echo '</ul>';
        closedir($handle);
        }   
}
else{
// Include some input validation here to see if $album is actually a subfolder of pics
    $album = $_GET['album'];
        if (!isset($_GET['i']))
            $i = 0;
        else
            $i = $_GET['i'];
    $ip = $i-1;
    $in = $i+1;
    $images = dirContents($album);
    $len = count($images);
    echo "<div class=\"subhead\">$album, Num photos = $len</div>";
    echo '<div class="viewer">';
        if ($ip < 0)
            echo '<a href="?page=gallery">Albums</a>';
        else
            echo "<a href=\"?page=gallery&album=$album&$ip\">Albums</a>";
    echo "<img src=\"$album\\$images[$i]\" />";
        if ($in >= count($album))
            echo '<a href="?page=gallery">Albums</a>';
        else
            echo "<a href=\"?page=gallery&album=$album&$in\">Next</a>";
    echo '</div>';
}

echo 'All images appear here with the given consent of those persons that appear within them';
?>
deceze
  • 510,633
  • 85
  • 743
  • 889
Jonno_FTW
  • 8,601
  • 7
  • 58
  • 90

1 Answers1

4

./pics is a directory name relative to the current working directory (./), you cannot use this unless you explicitly used chdir to navigate to that directory beforehand, you must supply an absolute directory path to opendir.

opendir(dirname(__FILE__) . '/pics');
Jacob Relkin
  • 161,348
  • 33
  • 346
  • 320
  • 1
    You can obtain the directory of the current file by doing `dirname(__FILE__)` (or `__DIR__` if you are using 5.3+) – Christian Joudrey Dec 10 '10 at 04:31
  • When I use PATH_SEPARATOR, it returns a semicolon ';'. So I get the warning: `Warning: opendir(C:\php;pics,C:\php;pics): The system cannot find the file specified. (code: 2) in C:\php\gallery.php on line 21 ` – Jonno_FTW Dec 10 '10 at 05:49