Can anyone see anything wrong with the following code. I'm a complete beginner and I've been following this tutorial, I'm currently stuck with this section of code, when it's working it's supposed to create a menu listing pictures, but it isn't working. My initial thoughts are that it's something to do with the path but I've tried altering the path and it still doesn't work, I have also manually inserted a picture from the same location on a page and the path for the said picture is exactly the same as the code in the following example so I'm at a loss as to why I cannot get this working any help would be appreciated. (be gentle with me )
td><select name="picture" id="picture">
<option value="">Select an image</option>
<?php
// Execute code if images folder can be opened, or fail silently
if ($imageFolder = @opendir("../images/")) {
// Create an array of image types
$imageTypes = array('jpg','jpeg','gif','png');
// Traverse images folder, and add filename to $img array if an image
while (($imageFile = readdir($imageFolder)) !== false) {
$fileInfo = pathinfo($imageFile);
if (in_array($fileInfo['extension'],$imageTypes)) {
$img[] = $imageFile;
}
}
// Close the stream from the images folder
closedir($imageFolder);
// Check the $img array is not empty
if ($img) {
// Sort in natural, case-insensitive order, and populate menu
natcasesort($img);
foreach ($img as $image) {
echo "<option value='$image'>$image</option>\n";
}
}
}
?>
Triple check the path (../images/). Make sure the script has read permissions on the images dir too. The code as it stands works ok for me (once i'd hit on a correct image path).
Remove that @ in front of opendir. It is an error suppressor, which if you are not pointing to the right directory PHP wouldn't give you an error.
In case you didn't know, the "../" means one directory above where your script is running. So if your script was in directory a/b/ then ../images/ would tell the script to look for a/images.
Bookmarks