SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    stockport
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A dummy asks.....

    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";
    }
    }
    }
    ?>

  2. #2
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What error does PHP give you?

  3. #3
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one too many equal signs?

    while (($imageFile = readdir($imageFolder)) !== false) {

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    stockport
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not getting any errors......

  5. #5
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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).

  6. #6
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •