SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,638
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    display onlyimages in adirectory

    This script displays the contents of my directory, problem s it displays the strange directorie(., ..)
    PHP Code:

    <?php

    $dirname 
    'images/'.$row['user_id'].'/';
    $images scandir($thumbsdirname);
    foreach(
    $images as $curimg){
      echo 
    "<a class='fancybox' href='".$dirname.$curimg."' data-fancybox-group='gallery' title='".$row['name']."'><img src='".$dirname.$curimg."' alt='' /></a>\n";
        }; 
    ?>
    How do I change it to only display the image in the directory?

    Thx
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,638
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Got, had to create a array of accepted extensions, then I had to add an if statement to see if I either got a., a .. or one other extension.
    PHP Code:
    <?php

    $thumbsdirname 
    'images/'.$row['user_id'].'/thumbs/';
    $dirname 'images/'.$row['user_id'].'/';
    $file_types = array ('jpg''jpeg''png''gif');
    $images scandir($thumbsdirname);
    foreach(
    $images as $curimg){
        
    $file_type strtolower(end(explode('.'$curimg)));
        if (
    $curimg !=='.' && $curimg !== '..' && in_array($file_type$file_types) == true)
        { 
    echo ...
        }
        }; 
    ?>
    "Oh, and Jenkins--apparently your mother died this morning."

  3. #3
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    A new way:
    PHP Code:
    <?php

    $directory 
    = new DirectoryIterator(__DIR__);
    $file_types = array ('jpg''jpeg''png''gif');
    foreach (
    $directory as $fileinfo) {
        if (
    $fileinfo->isFile() && in_array($fileinfo->getExtension(), $file_types) ) {
            echo 
    $fileinfo->getFilename() . "\n";
        }
    }

    ?>


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
  •