Displaying images

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
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Displaying images

Post by nick2 »

Hello.. quick question..

how would I display all images of a folder ?

I'm putting each image into a checkbox form.. so you can pick the one you want to remove..

so <p><input type="checkbox" name="C1" value="ON"></p>

anyway help would be great thanks!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Quick answer:
Start by using opendir() to get all images... You should manage from that page's usercomments.
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

thank you!
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

Hey I need to save the login id they type into a cookie but I am already setting cookies so I don't tink I can set two..

any suggestions?


see I need the login id to know what folder their images are in.

lol

thanks
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Uhm, yah.
You can use more than one cookie...
setvookie()

And, read the text in my signature.

Code: Select all

<?php
// an example from the manual...

// set the cookies
setcookie ("cookie[three]", "cookiethree");
setcookie ("cookie[two]", "cookietwo");
setcookie ("cookie[one]", "cookieone");

// after the page reloads, print them out
if (isset($_COOKIE['cookie'])) {
    foreach ($_COOKIE['cookie'] as $name => $value) {
        echo "$name : $value <br />\n";
    }
}

/* which prints

three : cookiethree
two : cookietwo
one : cookieone

*/
?>
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

Thanks again but how would I go about making a loop to display all images?

i'm new at this..
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

There are examples on the opendir-manual-pages. This is what I have done once:

Code: Select all

$dir = './images'; // set directory
$dh = opendir($dir); // open it for reading
echo '<select name="image">'; // html
while (($file = readdir($dh)) !== false) { // loop files
   if (substr($file,-4) == '.jpg') { // just fetch .jpg images...
        echo "\n".' <option>' . $file .'</option>'; // each image html
     }
}
echo '</select>'; // end html
Customize to fit your needs...
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

could you please tell me whats wrong? i'm getting very confused..

Code: Select all

<html>

<head>
<title>Slices.net - Manage pictures</title>
</head>

<body>

<table border="1" width="100%" bordercolor="#000000" cellspacing="1" cellpadding="0" bordercolorlight="#000000" bordercolordark="#000000">
  <tr>
    <td width="100%">
      <p align="center"><font size="2"><font color="#FF0000"><a href="addp.php">Add</a>
      a picture </font>/ <font color="#FF0000"><a href="removep.php">Remove</a></font>
      <font color="#FF0000">
      selected</font></font></td>
  </tr>
  <tr>
    <td width="100%">&nbsp;</td>
  </tr>
</table>
<form method="POST" action="removep.php">
<? $dir = $_COOKIE[user];
$dh = opendir($dir); // open it for reading 
while (($file = readdir($dh)) !== false) { // loop files 
        echo "<img border="0" src="$file">"; // each image html 
     } 
?>
</form>

</body>

</html>
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

for some reason its displaying 3 broken links.. :/

even thought theres 1 image.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

#1: You should add more quotes around the cookie variable...

Code: Select all

// bad
$dir = $_COOKIE[user];
// better
$dir = $_COOKIE['user'];
#2: readdir() also 'sees' the . (present dir) and .. (parent dir) in the catalogue, so you need to skip those with an if-clause. (as they are no images, hence can't be displayed). I used substr() to check if the $file ended with .jpg rahter than;

Code: Select all

if (($file != ".") and ($file != "..")) {
   // ... show image ...
 }
#3: Have in mind that you are trying to echo http://www.example.com/filename.jpg rather than http://www.example.com/username/filename.jpg

Code: Select all

// change...
        echo "<img border="0" src="$file">"; // each image html 
// to...
        echo "<img border="0" src="".$_COOKIE['username']."/".$file."">"; // each image html, added username-dir
User avatar
nick2
Forum Contributor
Posts: 118
Joined: Fri Jul 25, 2003 2:34 pm

Post by nick2 »

can you help with this? i'm trying to make this drop down menu work.. i want when u select the image in the drop down menu it will do post removep.php ... but for some reason it displays drop down menu then on right of that just a text box..

heres my code

Code: Select all

$dir = $_COOKIE[user];
$dh = opendir($dir); // open it for reading 
echo '<form><select name="delete">'; // html 
while (($file = readdir($dh)) !== false) { // loop files 
   if (substr($file,-4) == '.jpg') { // just fetch .jpg images... 
        echo "\n".' <option>' . $file .'</option>'; // each image html 
     } 
} 
echo '<p><input type="submit" value="Delete" name="B1"></p>';
echo '</select></form>'; // end html
also i don't want it to just display .jpg.. i removed that but it then displays . and ... in the drop down menu aswell.. ahhhh
Post Reply