Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Problems w/ code I have to change that lists files in a dir

    I'm having some problems understanding and change some code I have to maintain that creates a list of files in a folder.

    This was orgininaly set up for a single folder with no sub folders I've added sub folders but do not want them listed in the displayed table of files, just a list of files (or thumbnails as is the case).

    below are the two functions that build the table (file/directory) list, part of what's got me confused is the preg_match line in the GetDirList function I think thats stripping out . and .. but not sure (I've read and read docs but it isn't sinkin in)

    if some one could explain that to me and also point me in the right direction of striping out the folder names I'd be most gratefull.

    PHP Code:
    function BuildImageTable($Directory) {
      
    $p_Files GetDirList($Directory);
      
    $sTableFormat "<table border=\"3\" cellspacing=\"0\" cellpadding=\"20\">{ROWS}</table>";
      
    $sRows "";
      
    $NumCols 3;
      for(
    $iRow 0$iRow <= count($p_Files) / $NumCols$iRow++) {
        
    $sRows .= "<tr>";
        for(
    $iCol 0$iCol $NumCols$iCol++) {
          
    $iElem $iRow $NumCols $iCol;
          if(
    $iElem >= count($p_Files)) {
            break;
          }else{
            
    $sRows .= "<td valign=bottom><center><a CLASS=\"h3\" HREF=\"$Directory/{$p_Files[$iElem]}\" TARGET=\"_blank\"><IMG SRC=\"$Directory/Thumbnails/{$p_Files[$iElem]}\" BORDER=\"0\"></a></center>
                     <center>{$p_Files[$iElem]}</center><center><a CLASS=\"h3\" HREF=\"Upload.php?value={$p_Files[$iElem]}&dir=$Directory\" TARGET=\"_top\"> Delete</a></center>"
    ;
          }
        }
        
    $sRows .= "</tr>";
      }
      return 
    str_replace("{ROWS}"$sRows$sTableFormat);

    PHP Code:
    function GetDirList($dir) {
      
    $dl = array();
      if (
    $hd opendir($dir)) {
        while (
    $sz readdir($hd)) {
          if (
    preg_match("/^\./",$sz)==0) {
            
    $dl[] = $sz;
          }
        }
        
    closedir($hd);
      }
      
    asort($dl);
      return 
    $dl;

    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you most likely want to ignore . .. but that regex ignores anything beginning with a period (which may or may not be desired).

    I would change it to:
    PHP Code:
    while ($sz readdir($hd)) {
        if (
    preg_match("/^\.\.?$/",$sz) || is_dir($sz))
            continue;
        
    $dl[] = $sz;


  • #3
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by fci
    you most likely want to ignore . .. but that regex ignores anything beginning with a period (which may or may not be desired).

    I would change it to:
    PHP Code:
    while ($sz readdir($hd)) {
        if (
    preg_match("/^\.\.?$/",$sz) || is_dir($sz))
            continue;
        
    $dl[] = $sz;

    Thanks fci, I kind of understand what you mean about it would ignores anything beginning with a period (which could be bad).

    Still don't really understand what ("/^\.\.?$/",$sz) is doing/means
    And still not sure how to stop folders/directories from being displayed
    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians

  • #4
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ^ => beginning of string
    \. => a period
    \.? => a period that is optional
    $ => end of string

    in english:
    a string that begins with a period and optionally has another period, then the string will end.
    which matches:
    . or ..

    I added is_dir($sz) which meant it would continue and not add a dir to the array.
    Last edited by fci; 02-17-2006 at 06:27 AM.

  • #5
    Regular Coder musher's Avatar
    Join Date
    Jan 2005
    Location
    Minnesota
    Posts
    203
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    fci,
    Thanks for the help and thanks big time for the explaning

    your my new hero

    actualy fond a problem with the snipit (stuff occasionaly sinks in), I hope I got this right it seams to work ok.

    1. found out didnt have to check for . or .. when using is_dir()
    2. must include path with is_dir()

    PHP Code:
        while ($sz readdir($hd)) {
          if (
    is_dir($dir.$sz))
            continue;
          
    $dl[] = $sz;
        } 
    Last edited by musher; 02-17-2006 at 05:55 PM.
    Thanks
    Jim M

    "Lord, help me to become the person my dog thinks I am" - Dawn Ewing
    "If you must know. Yes, I do enjoy running after the dog sled when I fall off" - Me

    www.huskyzone.com -- Woodland Siberians


  •  

    Posting Permissions

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