Ok im not sure if this is the right place to put this but ... meh.
So i thought id share what ive been working on... you guys have helped me so hopefully this can come in useful to somebody.
My aim for this (slightly messy) code was for it to be used in an intranet. Not everyone here is good with a computer so i want it as easy as possible and as familiar as possible for them to use. Thus... i set out to make it like so:
- User makes a folder in the 'shared' folder. This new folder will become the album name.
- User adds picture files to folder. Easy.
- PHP script picks up folder name and lists it as an album
- When album name clicked, PHP sends which album it is to 'shared.php', who then gets all of the pictures from the folder, and displays them(using lightbox if you like), aswell as the file name underneath each one.
Simple & effective.
The code:
the links page will have this code:
<?php
// This line picks up all the folders in the shared folder. Modify the path accordingly.
foreach(glob('shared/pictures/*', GLOB_ONLYDIR) as $dir) {
// This line removes the unnecessary file path (in this case shared/pictures/.. modify this accordingly.
$dir = str_replace('shared/pictures/', '', $dir);
// This bit rebuilds the full path in a different variable..
$d = "shared/pictures/".$dir."/";
// This counts all of the image files in the folder... the GLOB_BRACE flag turns the {jpg,jpeg} etc.. into something that PHP can actually read and use.
$count = count(glob("".$d."*.{jpg,jpeg,JPG,JPEG,bmp,BMP,gif,GIF,png,PNG}", GLOB_BRACE));
// Now start to output everything..
echo '<tr><td>
// We are posting the form to shared.php where the images will be displayed. Modify accordingly.
<form name="shared" method="post" action="shared.php">
// A hidden input field holds the album 'name' (aka the folder name), ready for processing via the next page.
<input type="hidden" name="dir" id="dir" value="'.$d.'">
// Making the submit button look less button-like... And more album-title like.
<input type="submit" style="background:none; border:0; text-decoration:underline; font-size:14px; cursor:pointer" value="'.$dir.'">
</form></td><td>
// This bit tells you how many pictures there are in the folder.
- '.$count.' pictures.<br></td>';
}
?>
So thats the links page.
Here is the album displaying page.. thingy.
<?php
// Here we are getting the album to get the pictures from (it was posted in a hidden input field on the previous page)
$dir = $_POST['dir'];
// We are getting rid of the file path and the slashes to display just the folder name (album name)
$t = str_replace('shared/pictures/', '', $dir);
$title = explode('/', $t);
// Here we give a title.. which album you are viewing
echo "Viewing album: <b>$title[0]</b><br><br>";
// Here we tell it which files to pick up.. much like the last page where we counted how many there are.
$images = glob("".$dir. "*.{jpg,jpeg,JPG,JPEG,bmp,BMP,gif,GIF,png,PNG}", GLOB_BRACE);
// Begin a loop of what to do with each image. The str_replace is getting rid of the filepath so that we can display the image properly.
foreach($images as $image) {
$pic = str_replace($dir, '', $image);
// Here we are deciding what to do with the pictures. I have displayed them and hyperlinked each image so that when you click it it comes up full size (using lightbox) so modify this bit accordingly.
echo "<div align=\"center\"><a href=".$image." rel=\"lightbox\"><img src=".$image." width=\"400\"></a><br>
".$pic."<br></div>";
}
?>
That is all. Hope it is useful to somebody.
Regards
Josh