#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Sydney, NSW, Australia
    Posts
    40
    Rep Power
    14
    Hi there,

    I am trying to get PHP to list all the files in a directory to make a selection list to edit templates. Anyway, I can get the code to make the list, but it also adds two things to it. A '.' and a '..' - I don't want these two dot things in there

    The code I am using is...

    print "<form action=edit-templates.php method=post>";
    print "<select name=filetoedit>";
    $handle=opendir($templatedir);
    echo "Current Directory = $templatedir<br>n";
    echo "Files:n";
    while (($file = readdir($handle))!==false) {
    echo "<option value=$file>$file</option>";
    }
    closedir($handle);
    print "</select><input type=hidden name=action value=edittemplate><input type=submit value=Sumbit></form>


    That isn't the entire script, but only the important part. What would I add to only make it show things with extenions .inc and .css - or anything with an extension really?

    Thanks for your help!

    ------------------
    Michael Bray
    http://www.webhostarea.com
  2. #2
  3. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    Maybe the "next" function?
    I don't know the syntax for PHP but the two lines I added works fine in Perl.

    while (($file = readdir($handle))!==false) {
    next if $file eq ".";
    next if $file eq "..";
    echo "<option value=$file>$file</option>";
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Sydney, NSW, Australia
    Posts
    40
    Rep Power
    14
    Thanks!

    while (($file = readdir($handle))!==false) {
    if ($file=="."){

    } elseif ($file==".."){

    } else {
    echo "<option value=$file>$file</option>";
    }
    }

    Ended up doing the trick

    ------------------
    Michael Bray
    http://www.webhostarea.com
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jul 2000
    Posts
    669
    Rep Power
    14
    Just an FYI, but PHP has a little built-in dir reading function This would grab the .css and .inc files for you

    $templatedir = "c:/inetpub/wwwroot/tests";
    $dir = dir($templatedir);
    while ($file = $dir->read()) {
    $ext = substr($file, -4, strlen($file));
    if (($file != ".") && ($file != "..") && ((eregi("CSS", $ext)) &#0124; &#0124; (eregi("INC", $ext)))) {
    print $file."<br>";
    }
    }

    ?>
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Location
    Sydney, NSW, Australia
    Posts
    40
    Rep Power
    14
    Thanks! That's what I need.

    ------------------
    Michael Bray
    http://www.webhostarea.com

Similar Threads

  1. PHP math problem
    By spank in forum PHP Development
    Replies: 2
    Last Post: February 11th, 2004, 06:58 AM
  2. problem in Uploading file of size more than 512k in php
    By ssomvanshi in forum PHP Development
    Replies: 3
    Last Post: February 7th, 2004, 05:32 AM
  3. PHP link opening problem
    By Real World in forum PHP Development
    Replies: 2
    Last Post: February 5th, 2004, 06:48 AM
  4. XML DOM and PHP Problem, not displaying wanted info
    By drazhargraig in forum PHP Development
    Replies: 2
    Last Post: February 3rd, 2004, 03:14 PM
  5. fread problem: doesn t compile php code
    By ghorghor2002 in forum PHP Development
    Replies: 2
    Last Post: February 3rd, 2004, 05:25 AM

IMN logo majestic logo threadwatch logo seochat tools logo