PHP array to display a folder of JPG's?
Moderator: General Moderators
PHP array to display a folder of JPG's?
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
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
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!
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";
}
?>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?
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?
hmmm.
maybe i pasted the code wrong...
but this exact code works fine for me :
are you sure you dont have an unclosed bracket before my code?
can you paste your entire code, esp the bit before the php!
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";
}
?>can you paste your entire code, esp the bit before the php!
I'm confused on these lines here:
Wouldn't it be
As you are checking its equality?
Code: Select all
if ($handle = opendir(getcwd())) {
if ($handle = opendir('.')) {
//.....Code: Select all
if ($handle == opendir(getcwd())) {
if ($handle == opendir('.')) {
//......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 ..
EDIT:
ahhh!
i noticed a stupid error.
try this code
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);
}
?>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>";
?>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>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
here is the URL:
http://www.danenglander.com/phpshow/show.php
this is very odd :s
it works fine for me, what you just posted.
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
at the begginning of the php
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>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
?>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
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
are those results from:
?
if so, on your second linux server, try this:
that might work...
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>";
}
?>