Results 1 to 2 of 2

Thread: displaying all images in a dir

  1. #1
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default displaying all images in a dir

    so i found a bit of code to display all images in a chosen directory, it goes something like this:
    PHP Code:
              <?php
        $dir 
    '/r/';
        
    $imgs = array();

        if (
    $dh opendir($dir)) {
            while ((
    $file readdir($dh)) !== false) {
                if (!
    is_dir($file) && preg_match("/\.(bmp|jpe?g|gif|png)$/"$file)) {
                    
    array_push($imgs$file);
                }
            }

            
    closedir($dh);
        } else {
            die(
    'cannot open ' $dir);
        }

        foreach (
    $imgs as $idx=>$img) {
            
    $class = ($idx == count($imgs) - ' class="last"' '');
            echo 
    '<img src="' $dir $img '" alt="' 
                 
    $img '"' $class ' />' "\n";
        }
    ?>
    but i keep getting the 'cannot open' message. my images are in mywebsite.com/r/ so i set the dir as /r/ but i can't figure out why it's not able to open the dir. any ideas?

  2. #2
    Join Date
    Dec 2011
    Posts
    49
    Thanks
    8
    Thanked 1 Time in 1 Post

    Default

    well i figured out an alternative pretty quick, so i guess this thread is pretty useless now lol. since i store the urls in a db i used:
    PHP Code:
     require_once("../functions/connection.php");
      
        
    $result mysql_query("SELECT * FROM store"$connection);
        if(!
    $result){
            die(
    "query failed");
        }
        while(
    $row mysql_fetch_array($result)){
            echo 
    "<img src=\"http://www.mywebsite.com/".$row["url"]."\"><br />";
        }
    ?> 
    seems like a better way. perhaps the best way? self resolved thread!

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
  •