Page 1 of 1
PHP array to display a folder of JPG's?
Posted: Sat Feb 28, 2004 10:06 pm
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
Posted: Sat Feb 28, 2004 10:24 pm
by d3ad1ysp0rk
It's very possible.
php.net could help you out

Posted: Sun Feb 29, 2004 1:56 pm
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";
}
?>
Posted: Sun Feb 29, 2004 3:26 pm
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;
Posted: Mon Mar 01, 2004 1:42 am
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?
Posted: Mon Mar 01, 2004 9:40 am
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?
Posted: Mon Mar 01, 2004 12:17 pm
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!
Posted: Mon Mar 01, 2004 1:39 pm
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?
Posted: Mon Mar 01, 2004 1:46 pm
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>";
?>
Posted: Mon Mar 01, 2004 8:18 pm
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>
Posted: Mon Mar 01, 2004 8:20 pm
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
Posted: Tue Mar 02, 2004 2:00 am
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:
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
Posted: Tue Mar 02, 2004 9:59 am
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
Posted: Wed Mar 03, 2004 9:42 am
by toms100
are those results from:
?
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...
Posted: Fri Mar 05, 2004 12:32 pm
by Techx
this script is very cool, thanks