Page 1 of 1

get images from directory for gallery

Posted: Thu Sep 02, 2004 8:45 pm
by bradles
Hi all,

I am starting out on a script to try and pull jpegs from a thumbnail directory and display them on a page. I have used some code from the php manual here to get me started but I don't think the script is able to access the directory from what I've coded. All the images are in a subfolder called "images" and the thumbnails in a subfolder called "thumbnails".

I get the following error when I try to run the page.

Code: Select all

Warning: opendir(/images): failed to open dir: Invalid argument in G:\ApacheServer\PrintedVisions_August2004\Testing\clients\testclient\TMP8oiu63fzc6.php on line 15
This error is on my line of code that reads:
if ($handle = opendir("/" . $image_path)) {

Code: Select all

<?php 
$image_path 	= "images";
$thumbnail_path	= "thumbnails";
$extentions 	= array('jpg', 'jpeg', 'JPG', 'JPEG');

if ($handle = opendir("/" . $image_path)) &#123;
   echo "Directory handle: $handle\n";
   echo "Files:\n";

   /* This is the correct way to loop over the directory. */
   while (false !== ($file = readdir($handle))) &#123; 
       echo "$file\n";
   &#125;

closedir($handle); 

&#125;

?>
I would appreciate any help at all with this. I know I'm starting out and have a long but rewarding road ahead of me.

Thanks.

Brad.

Posted: Thu Sep 02, 2004 8:53 pm
by feyd
/images doesn't exist. You need a path that does exist.. The path to use is related to the physical location on the disk, rather than to the website.

Posted: Thu Sep 02, 2004 9:14 pm
by bradles
Thanks Feyd,

So I need to be more explicit with the path using the opendir. I used the getcwd() function to get the current directory and then just appended the "/thumbnails" to it.

Then using "/thumbnails" in the echo statement to echo the images to the browser. The only thing is that it echo's two placeholders and then the images. I assume this is the . and ..

How do I stop it from doing that?

Code: Select all

<?php 
$extentions 		= array('jpg', 'jpeg', 'JPG', 'JPEG');

$currentdir			= getcwd();
$image_path			= $currentdir . "/thumbnails";

if ($handle = opendir($image_path)) &#123;

   /* This is the correct way to loop over the directory. */
   while (false !== ($file = readdir($handle))) &#123; 
       echo "<img src="thumbnails/$file">";
   &#125;

closedir($handle); 

&#125;

?>
Brad.

Posted: Thu Sep 02, 2004 9:16 pm
by feyd
only echo the filename if it not '.' or '..'

Posted: Thu Sep 02, 2004 9:20 pm
by bradles
Thanks.

I used:

Code: Select all

if (is_dir($file)) &#123;
	   		
		&#125;
		else &#123;
			echo "<img src="thumbnails/$file">";
		&#125;
Not that glamourous just yet but that will at least get me started.
Thanks again.

Posted: Thu Sep 02, 2004 9:44 pm
by bradles
Feyd,

Do you have any advice or know of any tutorials I could follow on how to keep the code separate to the html and styles used to display the images?

Brad.

Posted: Thu Sep 02, 2004 9:45 pm
by feyd
search for templates

Posted: Thu Sep 02, 2004 9:50 pm
by bradles
Thanks.

I'll get started.

Brad