Forum Moderators: coopster

Message Too Old, No Replies

getcwd() and opendir() commands

help with a gallery...

         

ahmedtheking

1:54 pm on Dec 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ive got this PHP gallery script that reads the folders in a directory via the 'getcwd()' command to generate a gallery.

I'm trying to link this gallery into my main file, via inlucde:

the index file is in the root dir, /home/firestar/public_html/ and the gallery file is in the images/gallery/ folder.

Ive tried changing the opendir to different dir, but the script doesnt work unless its in the gallery folder!

If that sounds a bit strange, heres the script:
<?php

$cwd = "/home/firestar/public_html/images/gallery"; // get current working directory << here is where its either getcwd() or the dir!

// create snapshot sub navigation

// search current working directory for snapshot folders
$handle = opendir("$cwd");

$folders = array();
$i = 0;

while (($directory = readdir($handle))!= false) {
// read snapshot folders.

if (
(is_dir($directory)) and // make sure $directory is a folder, not a file
($directory!= ".") and// don't read hidden folders
($directory!= "..")
) {

$folders[$i] = "$directory"; // add folder name to array
$i++;
}
}

sort($folders); // sort folder names alphabetically
reset($folders); //set array to read from the start after sorting

// create links with folder names. Format into list.
$snap_sub_nav = "<p>Below are a few galleries showing off our websites, graphic and photographic skills! Feel free to browse through them! The images/photos/graphics that are
associated with our clients will have links to take you to the client's case study. <span class=\"bold\">Enjoy!</span></p>
<div id=\"gallery_list\"><ul>
";

foreach($folders as $value) {
$snap_sub_nav .= "<li><a href=\"gallery.php?dir=".urlencode("$value")."\" title=\"View the $value Gallery\">$value</a></li>\n";
}
$snap_sub_nav .= "</ul></div>\n";
// end snapshot sub navigation


// set snapshot directory

if ((isset($_GET["dir"])) and (is_dir($cwd."/".$_GET["dir"]))) {
$dir = strip_tags(urldecode($_GET["dir"])); // directory selected for viewing
} else {
$dir = ""; // no directory chosen - display snapshot home page.
}

// Read blurb.html in snapshot directory.
if (file_exists($cwd."/".$dir."/blurb.html")) {
$blurb_file = $cwd."/".$dir."/blurb.html"; //create full path to blurb.html
$blurb_handle = fopen($blurb_file,"r"); // open blurb.html for reading
$blurb = fread($blurb_handle, filesize($blurb_file)); // read blurb.html
fclose($blurb_handle); // close file
}
// build page content

// thumbnails or images

if ($dir!= "") { // snapshot directory is selected - display thumbnails

// get imaage file names
$img_handle = opendir($cwd."/".$dir."/thumbs/");

$img_list = array();
$i = 0;

while (($img = readdir($img_handle))!= false) {
if (
(!is_dir($img)) && // don't read folders
(strpos("$img",".")!= 0) &&// don't read hidden files
((stristr($img,".jpg")==true) && (stristr($img,".jpg.LCK")==false))
) {

$img_list[$i] = $img; // add image to image array
$i++;

}
} // end while

// check for pic
if (!isset($_GET["pic"])) { // no pic chosen, show thumbnails

$thumb_col = 0;
foreach($img_list as $key => $value) {

$size = @getimagesize($cwd."/".$dir."/thumbs/$value"); // get image size

$thumbs .= "
<li>
<a href=\"gallery.php?dir=".urlencode($dir)."&amp;pic=".urlencode($key)."\" title=\"$value\">
<img src=\"$dir"."/thumbs/$value\" {$size[3]} border=\"0\" alt=\"$value\">
</a>
</li>\n
";

/* if ($thumb_col >=3) { // break thumbs table into 4 column rows
$thumbs .= "\n</tr>\n<tr>";
$thumb_col = 0;
} else {
$thumb_col++;
} */

}

// format thumbnail table
$thumbs_table = "
<div id=\"gallery_thumbs\"><ul>
$thumbs
</ul></div>
";

} else {// pic chosen, display with previous/next/back links

$pic = strip_tags(urldecode($_GET["pic"]));

$links = "<p>";// begin next/prev links

//display pic with prev and next links
if ($pic > 0) { //set prev link
$links .= "
<a href=\"gallery.php?dir=".urlencode($dir)."&amp;pic=".urlencode(($pic-1))."\" title=Previous Photo>
Previous</a> ¦
";
}

// back to thumbnails link
$links .= "<a href=\"gallery.php?dir=".urlencode($dir)."\" title=Back to Contact Sheet>
Back</a> ¦ <a href=\"buy.php?dir=".urlencode($dir)."&amp;pic=".urlencode(($pic))."&folder=Stones\" title=Buy a Print of this photo>Buy Prints</a>";

if ($pic < (count($img_list)-1)) {
$links .= "
¦ <a href=\"gallery.php?dir=".urlencode($dir)."&amp;pic=".urlencode(($pic+1))."\" title=Next Photo>
Next</a>";
}

$links .= "</p>"; // close next/prev links

// get pic size
$size = @getimagesize($cwd."/".$dir."/".$img_list["$pic"]);
$folder = "Stones";
$image .= "
$links
<img src=\"$dir/".$img_list["$pic"]."\" {$size[3]} border=\"0\" alt=\"$pic\" title=\"$pic\">
";

} // end if pic

}// end if dir

// determine page display

if (($dir!= "") and ($pic!="")) { //display image with links

echo $image;

} else if (($dir!= "") and ($pic =="")) { // display thumbnails of chosen snapshot folder

echo $thumbs_table;

} else { // display snapshot gallery home

echo $snap_sub_nav;

}

?>

I had a look at this post to try and help me, but it didnt help!

[webmasterworld.com...]

jatar_k

4:07 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what exactly is the behaviour? 'doeasn't work' isn't much to go on.

ahmedtheking

7:05 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well unless the script is in the /images/galleries/ folder, no images are picked up by the script. No errors are passed either. It's just blank under the sub_nav text.

ergophobe

7:15 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It looks like it isn't finding any directories. After you get your list of directories and sort and reset, try to just output the folder array and stop

print_r($folders);
exit();

Let's see what you have there. If that's blank, comment out your "if" conditions and just collect everything, files, directories, and . and .. and then try again.

Tom

ahmedtheking

8:00 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah it works! Thank you!

I used your command to get the arrays. From then, i searched the PHP manual for getcwd alts and came accross chdir.

So now its like this:

getcwd();
chdir('images/gallery');

$cwd = getcwd();

where it gets the current dir and then changes it according to where you want it to go!

thanks!:D

ergophobe

8:05 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Excellent. Remember, print_r() is your friend!

ahmedtheking

8:20 pm on Dec 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lol! I think i need to start using 'else error' commands more!