SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Location
    Seattle, Washington, United States
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post Images From A Directory Dynamically

    Hello All,

    What I'd like to do is grab all images from a folder and post them dynamically to a web page. This code works fine if I hardcode in the exact folder, but the tricky part is getting it to work for all folders by looking at the URL and stripping it of the .php extension, which always matches the folder name exactly (a variable I have called $topic).

    Does anyone have any ideas how to get this to work? I am fairly new to programming in general so I apologize if this isn't very clear.

    Here is the code:

    <?php

    $topic = basename($_SERVER['PHP_SELF'],".php").PHP_EOL;
    $files = glob('img/' . $topic . '/*.*');

    for ($i=0; $i<count($files); $i++) {
    $num = $files[$i];
    echo '<a href="'.$num.'" target="_blank">'
    .'<img class="thumb" src="'.$num.'">'
    .'</a>&nbsp;&nbsp;';
    }

    ?>

  2. #2
    SitePoint Zealot Sogo7's Avatar
    Join Date
    May 2011
    Posts
    129
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This gallery demo may be of some help as a starting point.
    http://www.dynamicdrive.com/dynamici...photoalbum.htm

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Location
    Seattle, Washington, United States
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Sogo! I wasn't able to get the sample code to work, but it is a good starting point.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I don't think you need PHP_EOL appended to the $topic though, that just adds an OS independent line end.

  5. #5
    SitePoint Member
    Join Date
    Jan 2013
    Location
    Seattle, Washington, United States
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep, you're right. Thanks!


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
  •