get images from directory for gallery

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bradles
Forum Commoner
Posts: 89
Joined: Wed Jun 30, 2004 10:40 pm

get images from directory for gallery

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
bradles
Forum Commoner
Posts: 89
Joined: Wed Jun 30, 2004 10:40 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

only echo the filename if it not '.' or '..'
bradles
Forum Commoner
Posts: 89
Joined: Wed Jun 30, 2004 10:40 pm

Post 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.
bradles
Forum Commoner
Posts: 89
Joined: Wed Jun 30, 2004 10:40 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

search for templates
bradles
Forum Commoner
Posts: 89
Joined: Wed Jun 30, 2004 10:40 pm

Post by bradles »

Thanks.

I'll get started.

Brad
Post Reply