PHP array to display a folder of JPG's?

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
User avatar
Brewster
Forum Newbie
Posts: 6
Joined: Sat Feb 28, 2004 10:06 pm

PHP array to display a folder of JPG's?

Post by Brewster »

I am looking to do a PHP web page that draws from a folder of JPG's.

I think it is some kind of array that will display the photos one after another.  I don't want a "random script".  

I have the idea of an HTML "forward" and "back" button or text. The idea is that the client can always upload new images anytime he wants keeping to a standard of say about 30 images at any given time.  I figured with an array they could be numered like "1_name-a.jpg", 2_name-whatever.jpg", etc..

I don't know alot about PHP so I am pretty new to this.  I do know ASP.  This will be on a server that has PHP 4.  I don't have access to a database though.

Is this possible?

Dan
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

It's very possible.

php.net could help you out :)
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

Very quick script i nocked up for you
needs some checking such prev picture may be less than 0.
EDIT: made it so it works without needing a dir specified, and can be named as anything you want!
just put the phpfile and the images in the same dir. (and nothing else!)
EDIT2: Finished now, it will only list image files! see code on how to add other extensions!

Code: Select all

<?php
/// Load all images into an array referencing their ID to their image name
if ($handle = opendir(getcwd())) {
	if ($handle = opendir('.')) {
		$i = 1;
	  	while (false !== ($file = readdir($handle))) {
		   	if ($file != "." && $file != "..") {
				$t = explode(".",$file);
				if ($t[1] == "JPG" || $t[1] == "JPEG" || $t[1] == "jpg" || $t[1] == "jpeg" || $t[1] == "gif" || $t[1] == "GIF") // Add extra file extensions here
				{
					$img[$i] = $file;		
					$i++;// inc $i			  
				}
				
		   }
	   }
	   $i = $i - 1; // Removes the excess i.
	   closedir($handle);
	}
	if (isset($_GET['picture_id'])) {
		/// The User has chosen a picture
		$id = $_GET['picture_id'];
		} else {
		// start at the beginning
		$id = 1;
		}
	echo "<b>Showing Picture ".$id." of ".$i."</b><br>";
	echo "<img src="".$img[$id].""><br>";
	$next_id = $id + 1;
	$prev_id = $id - 1;
	if ($next_id <= $i)
		echo "<a href="".$_SERVER['PHP_SELF']."?picture_id=".$next_id."">Next</a>";
	echo " | ";
	if ($prev_id >= 1 )
		echo " <a href="".$_SERVER['PHP_SELF']."?picture_id=".$prev_id."">Previous</a>";
	} else {
	echo "Cannot open dir";
	}
?>
User avatar
Brewster
Forum Newbie
Posts: 6
Joined: Sat Feb 28, 2004 10:06 pm

Post by Brewster »

Hi Toms, thanks!:

I am getting this error:

/usr/local/www/virtual2/66/175/45/246/html/show/rotate4.php on line 14

That code would be:

13 if ($handle = opendir(getcwd())) {
14   if ($handle = opendir('.')) {
15     $i = 1;
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

hmm are you sure that is the whole error?
i think there should be a line before that .. :/
what are you hosting on?
User avatar
Brewster
Forum Newbie
Posts: 6
Joined: Sat Feb 28, 2004 10:06 pm

Post by Brewster »

You got me thinking about the hosting company. It is cedant.com.

I decded to try the script on another server and now I get this:

PHP Parse error: parse error, unexpected T_STRING in E:\web\danenglande\htdocs\phpshow\show.php on line 14
This is on a windows server the former on UNIX

I did some research and apparently the PHP error isn’t always in the line the message points to.

The error may indicate that some quotes are misplaced?
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

hmmm.
maybe i pasted the code wrong...
but this exact code works fine for me :

Code: Select all

<?php
/// Load all images into an array referencing their ID to their image name
if ($handle = opendir(getcwd())) {
	if ($handle = opendir('.')) {
		$i = 1;
	  	while (false !== ($file = readdir($handle))) {
		   	if ($file != "." && $file != "..") {
				$t = explode(".",$file);
				if ($t[1] == "JPG" || $t[1] == "JPEG" || $t[1] == "jpg" || $t[1] == "jpeg" || $t[1] == "gif" || $t[1] == "GIF") // Add extra file extensions here
				{
					$img[$i] = $file;		
					$i++;// inc $i			  
				}
				
		   }
	   }
	   $i = $i - 1; // Removes the excess i.
	   closedir($handle);
	}
	if (isset($_GET['picture_id'])) {
		/// The User has chosen a picture
		$id = $_GET['picture_id'];
		} else {
		// start at the beginning
		$id = 1;
		}
	echo "<b>Showing Picture ".$id." of ".$i."</b><br>";
	echo "<img src="".$img[$id].""><br>";
	$next_id = $id + 1;
	$prev_id = $id - 1;
	if ($next_id <= $i)
		echo "<a href="".$_SERVER['PHP_SELF']."?picture_id=".$next_id."">Next</a>";
	echo " | ";
	if ($prev_id >= 1 )
		echo " <a href="".$_SERVER['PHP_SELF']."?picture_id=".$prev_id."">Previous</a>";
	} else {
	echo "Cannot open dir";
	}
?>
are you sure you dont have an unclosed bracket before my code?
can you paste your entire code, esp the bit before the php!
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

I'm confused on these lines here:

Code: Select all

if ($handle = opendir(getcwd())) { 
   if ($handle = opendir('.')) {
//.....
Wouldn't it be

Code: Select all

if ($handle == opendir(getcwd())) { 
   if ($handle == opendir('.')) { 
//......
As you are checking its equality?
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

im not sure :S
i pinched that part of the code off the php website!
(i needed to remove the . and ..!)

Example 2. List all files in the current directory and strip out . and ..

Code: Select all

<?php
if ($handle = opendir('.')) {
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") {
           echo "$file\n";
       }
   }
   closedir($handle);
}
?>
EDIT:
ahhh!
i noticed a stupid error.
try this code :)

Code: Select all

<?php
/// Load all images into an array referencing their ID to their image name
if ($handle = opendir(getcwd())) {
	$i = 1;
	while (false !== ($file = readdir($handle))) {
		if ($file != "." && $file != "..") {
			$t = explode(".",$file);
			if ($t[1] == "JPG" || $t[1] == "JPEG" || $t[1] == "jpg" || $t[1] == "jpeg" || $t[1] == "gif" || $t[1] == "GIF") { 
				// Add extra file extensions here ^^^
				$img[$i] = $file;		
				$i++;// inc $i			  
			}					
		}
	}
   $i = $i - 1; // Removes the excess i.
   closedir($handle);
} else {
	echo "Cannot Read Directory";
}
if (isset($_GET['picture_id'])) {
	/// The User has chosen a picture
	$id = $_GET['picture_id'];
	} else {
	// start at the beginning
	$id = 1;
	}
echo "<b>Showing Picture ".$id." of ".$i."</b><br>";
echo "<img src="".$img[$id].""><br>";
$next_id = $id + 1;
$prev_id = $id - 1;
if ($next_id <= $i)
	echo "<a href="".$_SERVER['PHP_SELF']."?picture_id=".$next_id."">Next</a>";
echo " | ";
if ($prev_id >= 1 )
	echo " <a href="".$_SERVER['PHP_SELF']."?picture_id=".$prev_id."">Previous</a>";
?>
User avatar
Brewster
Forum Newbie
Posts: 6
Joined: Sat Feb 28, 2004 10:06 pm

Post by Brewster »

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>New  Works</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">



</head>
<body>
<?php 
/// Load all images into an array referencing their ID to their image name 
if ($handle = opendir(getcwd())) { 
   $i = 1; 
   while (false !== ($file = readdir($handle))) { 
      if ($file != "." && $file != "..") { 
         $t = explode(".",$file); 
         if ($t[1] == "JPG" || $t[1] == "JPEG" || $t[1] == "jpg" || $t[1] == "jpeg" || $t[1] == "gif" || $t[1] == "GIF") { 
             // Add extra file extensions here ^^^ 
            $img[$i] = $file;       
            $i++;// inc $i           
          }                
      } 
   } 
   $i = $i - 1; // Removes the excess i. 
   closedir($handle); 
} else { 
   echo "Cannot Read Directory"; 
} 
if (isset($_GET['picture_id'])) { 
   /// The User has chosen a picture 
   $id = $_GET['picture_id']; 
   } else { 
   // start at the beginning 
   $id = 1; 
   } 
echo "<b>Showing Picture ".$id." of ".$i."</b><br>"; 
echo "<img src="".$img[$id].""><br>"; 
$next_id = $id + 1; 
$prev_id = $id - 1; 
if ($next_id <= $i) 
   echo "<a href="".$_SERVER['PHP_SELF']."?picture_id=".$next_id."">Next</a>"; 
echo " | "; 
if ($prev_id >= 1 ) 
   echo " <a href="".$_SERVER['PHP_SELF']."?picture_id=".$prev_id."">Previous</a>"; 
?> 
 
 
	
</body>
</html>
User avatar
Brewster
Forum Newbie
Posts: 6
Joined: Sat Feb 28, 2004 10:06 pm

Post by Brewster »

the code above is my complete HTML page called "show.php" in a directory called "/phpshow" in which only this page and some .jpg files are.

here is the URL:

http://www.danenglander.com/phpshow/show.php
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

this is very odd :s
it works fine for me, what you just posted.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>New  Works</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">



</head>
<body>
<?php
/// Load all images into an array referencing their ID to their image name
if ($handle = opendir(getcwd())) {
   $i = 1;
   while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
         $t = explode(".",$file);
         if ($t[1] == "JPG" || $t[1] == "JPEG" || $t[1] == "jpg" || $t[1] == "jpeg" || $t[1] == "gif" || $t[1] == "GIF") {
            // Add extra file extensions here ^^^
            $img[$i] = $file;      
            $i++;// inc $i          
         }               
      }
   }
   $i = $i - 1; // Removes the excess i.
   closedir($handle);
} else {
   echo "Cannot Read Directory";
}
if (isset($_GET['picture_id'])) {
   /// The User has chosen a picture
   $id = $_GET['picture_id'];
   } else {
   // start at the beginning
   $id = 1;
   }
echo "<b>Showing Picture ".$id." of ".$i."</b><br>";
echo "<img src="".$img[$id].""><br>";
$next_id = $id + 1;
$prev_id = $id - 1;
if ($next_id <= $i) {
   echo "<a href="".$_SERVER['PHP_SELF']."?picture_id=".$next_id."">Next</a>";
   }
echo " | ";
if ($prev_id >= 1 ) {
   echo " <a href="".$_SERVER['PHP_SELF']."?picture_id=".$prev_id."">Previous</a>";
   }
?>


   
</body>
</html>
perhaps your php is in safe mode or something?
try a page with just this:

Code: Select all

<?php
echo getcwd();
?>


and paste me the output!
if that doesnt work, try

Code: Select all

<?php
if ($handle = opendir(getcwd())) {
   $i = 1;
/// etc etc
?>
at the begginning of the php
User avatar
Brewster
Forum Newbie
Posts: 6
Joined: Sat Feb 28, 2004 10:06 pm

Post by Brewster »

Here is 1st on Windows:
E:\web\danenglande\htdocs

2nd on Windows:
PHP Parse error: parse error, unexpected T_STRING in E:\web\danenglande\htdocs\test.php on line 13

1st on Unix (Linux?)
/usr/local/www/virtual2/66/175/45/246/html

2nd on Unix (Linux?)
Parse error: parse error in /usr/local/www/virtual2/66/175/45/246/html/test.php on line 12
toms100
Forum Contributor
Posts: 119
Joined: Wed Feb 26, 2003 10:29 am
Location: Bristol,UK

Post by toms100 »

are those results from:

Code: Select all

<?php
echo getcwd();
?>
?
if so, on your second linux server, try this:

Code: Select all

<?php
/// Load all images into an array referencing their ID to their image name
if ($handle = opendir('/usr/local/www/virtual2/66/175/45/246/html/')) {
   $i = 1;
   while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
         $t = explode(".",$file);
         if ($t[1] == "JPG" || $t[1] == "JPEG" || $t[1] == "jpg" || $t[1] == "jpeg" || $t[1] == "gif" || $t[1] == "GIF") {
            // Add extra file extensions here ^^^
            $img[$i] = $file;     
            $i++;// inc $i         
         }               
      }
   }
   $i = $i - 1; // Removes the excess i.
   closedir($handle);
} else {
   echo "Cannot Read Directory";
}
if (isset($_GET['picture_id'])) {
   /// The User has chosen a picture
   $id = $_GET['picture_id'];
   } else {
   // start at the beginning
   $id = 1;
   }
echo "<b>Showing Picture ".$id." of ".$i."</b><br>";
echo "<img src="".$img[$id].""><br>";
$next_id = $id + 1;
$prev_id = $id - 1;
if ($next_id <= $i) {
   echo "<a href="".$_SERVER['PHP_SELF']."?picture_id=".$next_id."">Next</a>";
   }
echo " | ";
if ($prev_id >= 1 ) {
   echo " <a href="".$_SERVER['PHP_SELF']."?picture_id=".$prev_id."">Previous</a>";
   }
?>
that might work...
Techx
Forum Newbie
Posts: 2
Joined: Fri Mar 05, 2004 8:38 am

Post by Techx »

this script is very cool, thanks
Post Reply