adam291086 Posted December 11, 2007 Share Posted December 11, 2007 I have a folder full of pictures. Is it possible to display all the folders content as thumbnails in one go. Without a database? If so what should i google. I have tried various search and really got nothing. Quote Link to comment Share on other sites More sharing options...
tapos Posted December 11, 2007 Share Posted December 11, 2007 first read the directory for files then make thumbnail view of the image and show the thumbnail image. Thanks -- Tapos Pal Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 how do i read a directory? I suppose once i read the directory i will need a while loop to say while there is informatio echo img scr=blah blah blah. Quote Link to comment Share on other sites More sharing options...
tapos Posted December 11, 2007 Share Posted December 11, 2007 u can use readdir function. see http://www.php.net/manual/en/function.readdir.php -- Tapos Pal Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 11, 2007 Share Posted December 11, 2007 This is the code which i am working, it reads the file and displays it, it is still not complete. Maybe you can modify it and use... (and later share with me ) <?php $dir = "homio/"; $i=0; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { $all_files[$i] = $file; $i++; } closedir($handle); } foreach ($all_files as $value) { $path = $dir.$value; print_r($value); echo '<img src="'.$path.'" width="250" height="150"><br />'; } ?> Quote Link to comment Share on other sites More sharing options...
delphi123 Posted December 11, 2007 Share Posted December 11, 2007 Here's the code I've developed to show it in a listbox. The only thing I've got to fix is stopping it showing the full directory location and just show the file titles in the list box - not sure if anyone can help me with this? ??? <select> <?php $ImageDirectory = "../Content/Reviews/images"; foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image) { echo "<option>".$image."</option>"; } ?> </select> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 neon i have used you script. It works well. The only problem is that it echo out two other images with the name . and .. When i look in the directory there is no such file. Why is this happening? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 11, 2007 Share Posted December 11, 2007 The files with the names "." and ".." are special directory files in the UNIX world, they point to the current directory and the parent directory respectively. Ken Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 11, 2007 Share Posted December 11, 2007 I could not remove that DOTS... I switched to Delphi's code, i found it more efficient... does the same thing, just modify TR and TD's to get the look as you want... <?php $images = "images"; foreach (glob("$images/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image) { echo "<table><tr>"; echo '<td><img src="'.$image.'" width="250" height="150"></td>'; echo "</tr></table>"; } ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 ok i have modified the code <?php error_reporting(E_ALL); $ImageDirectory = "../adam/cycling/admin/upload/pictures/thumbnails/"; foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image) { echo "<html>"; echo "<body>"; echo "<table><tr>"; echo '<td><img src="'.$image.'" width="250" height="150"></td>'; echo "</tr></table>"; echo "</html>"; echo "</body>"; } ?> Nothing is being echoed and i get no errors. Any ideas? Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 11, 2007 Share Posted December 11, 2007 verify you are getting data first <?php error_reporting(E_ALL); $ImageDirectory = "../adam/cycling/admin/upload/pictures/thumbnails/"; $files = glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE); print_r($files); ?> If taht returns an empty array you know your $ImagDirectory isn't right, try $_SERVER['DOCUMENTS_ROOT']."LOcATION" Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 ok i have changed to <?php error_reporting(E_ALL); $ImageDirectory = dirname(__FILE__)."/pictures/thumbnail/"; $files = glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE); print_r($files); ?> i know the direct is correct as that is where my upload script uploads the images to. All that's printed is array() Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 11, 2007 Share Posted December 11, 2007 lets just try <?php $ImageDirectory = dirname(__FILE__)."/pictures/thumbnail/"; $files = glob($ImageDirectory."*"); print_r($files); ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 that still only displays ''Aray ()'' Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 11, 2007 Share Posted December 11, 2007 try the last thing I posted with a folder path you know is good. this shoudl work and then just get contex from it <?php $dir = $_SERVER['DOCUMENT_ROOT']; $data = glob($dir."*"); print_r($data) Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 11, 2007 Share Posted December 11, 2007 This function will do the following, Select only the filetypes you specify. Sort the files according to filetype Show the image as a thumbnail And it is easy to use <?php function show_images($directory) { $images_types = array('gif','jpg','png'); foreach($images_types as images_type) { $images_files = glob($directory . "*." . $images_type); if($images_files != '') { foreach($images_files as $images_file) { print '<img src=' . $directory . $images_file . ' width=100px height=100px /><br />'; } } } } $images = show_images($_SERVER['DOCUMENT_ROOT']); if($images != '') { print $images; } ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 11, 2007 Author Share Posted December 11, 2007 ok i am using mr_mind code. The only thing echoed is . I am confuse with the code. Any help appreciated. <?php function show_images($directory) { $images_types = array('gif','jpg','png'); foreach($images_types as $images_type) { $images_files = glob($directory . "*." . $images_type); if($images_files != '') { foreach($images_files as $images_file) { print '<img src=' . $directory . $images_file . ' width=100px height=100px /><br />'; } } } } $images = show_images($_SERVER['DOCUMENT_ROOT']."/pictures/thumbnails/"); if($images != '') { print $images; } ?> Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 12, 2007 Author Share Posted December 12, 2007 ok my script has stopped working. <?php $dir = dirname(__FILE__)."/pictures/thumbnail"; foreach (glob("$dir/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image) { echo '<img src="'.$image.'" width="250" height="150">'; echo '<br />'; } ?> When i run the script it show 4 images boxes. Which is correct. The only problem is that the image itself is not being echoed. It just shows a red cross. http://adamplowman.co.uk/cycling/admin/upload/view_pics.php view whats happening here Quote Link to comment Share on other sites More sharing options...
~n[EO]n~ Posted December 12, 2007 Share Posted December 12, 2007 Your path is wrong i think ... full path is like this http://adamplowman.co.uk/cycling/admin/upload/pictures/thumbnail/BOTTOM-LINE.jpg (seen in the browser address bar) and http://adamplowman.co.uk/homepages/12/d214897219/htdocs/adam/cycling/admin/upload/pictures/thumbnail/BOTTOM-LINE.jpg this comes while checking image properties Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 12, 2007 Author Share Posted December 12, 2007 the problem is when i try and manually set the location it doesn't work at all. When is use dirname(_file_) it works. Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 12, 2007 Author Share Posted December 12, 2007 finally got it working. The code below shows how. How would i go about echoing the file name as well? <?php $ImageDirectory = "../upload/pictures/thumbnail/"; foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image) { echo "<table><tr>"; echo '<td><img src="'.$image.'"></td>'; echo '<td>$ImageDirectory</td>'; echo "</tr></table>"; } ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 12, 2007 Share Posted December 12, 2007 Try this (the glob returns a complete path so you need to get just the image name) <?php $ImageDirectory = "../upload/pictures/thumbnail/"; foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image) { echo "<table><tr>"; echo '<td><img src="'.$image.'"></td>'; echo '<td>$ImageDirectory</td>'; $temp = explode("/",$image); echo "<td>".$temp[count($count)-1]."</td>"; echo "</tr></table>"; } that should get you just the last item in the array exploded by the / removing all the folder contexes Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 12, 2007 Author Share Posted December 12, 2007 i did it the less complicated way, which i understand <?php $ImageDirectory = "../upload/pictures/thumbnail"; foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image) { //remove .. for url $dir = str_replace("..", "", "$ImageDirectory"); //get name for url $name = str_replace("$ImageDirectory", "", "$image"); //get name on it's own $fullname = str_replace("/", "", $name); echo '<br />'; echo $fullname; echo '<br />'; echo '<img src="'.$image.'">'; echo '<br />'; echo "www.adamplowman.co.uk".$dir.$name; echo '<br />'; } ?> Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted December 12, 2007 Share Posted December 12, 2007 yeah if you want that full path a better idea your way, that or what makes more sense (a universal one is) <?php $ImageDirectory = "../upload/pictures/thumbnail"; foreach (glob("$ImageDirectory/{*.gif,*.jpg,*.png,*.GIF,*.JPG,*.PNG}", GLOB_BRACE) as $image) { $path = str_replace($_SERVER['DOCUMENT_ROOT'],"",$image); echo '<br />'; echo $fullname; echo '<br />'; echo '<img src="'.$image.'">'; echo '<br />'; echo $path; echo '<br />'; } ?> That will work on any server instead of only yours, and its simpler Quote Link to comment Share on other sites More sharing options...
adam291086 Posted December 12, 2007 Author Share Posted December 12, 2007 ok, i can see what your doing. One last question. How would i go about deleting one pic from the directory? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.