Weird problem using is_dir()

Discussion in 'PHP' started by PoPSiCLe, Apr 4, 2009.

  1. #1
    I'm trying to get a listing of the subdirectories in a given folder - but for some reason the is_dir()-function only returns "." and ".." even though I know there are two subdirectories in the folder.

    The code I have is as follows:
    
    $img_folder = "images/";
    
    $list_dir = scandir($img_folder);
    
    foreach ($list_dir as $value) {
    	if (is_dir($value)) {
    		var_dump($value);
    		}
    }
    
    PHP:
    This outputs the following: string(1) "." string(2) ".."

    It should also list to other subdirectories, namely "medium" and "thumbs" - but it doesn't.

    If I try running the same code, but change the $img_folder variable to = "./" I get a full listing of the subdirectories in the root folder - so what am I doing wrong? It doesn't help adding "./" in front of the folder name either... it still just gives the "." and ".." - if I hadn't known there to be two folders, I would assume it listed an empty directory.
     
    PoPSiCLe, Apr 4, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The names you get from scandir() are not fully-qualified paths. You need to add the path to your call to is_dir() - for example:

    is_dir("{$img_folder}{$value}")
    Code (markup):
    or else you will just be comparing a list of filenames from "images" against the contents of your script's current working directory. It is not surprising that the only overlap is "." and ".."
     
    SmallPotatoes, Apr 4, 2009 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    Ah! Lovely, thanks alot man!

    That was... less than obvious, to me, at least :)
     
    PoPSiCLe, Apr 4, 2009 IP