Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: directory script

  1. #1
    Join Date
    Oct 2005
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default directory script

    I haveing some issues..

    I have a directory script and it shows
    .
    ..
    before the files..

    how do i get rid of it..

    Code:
    <?
    include("../include/session.php");
    ?>
    
    <?php
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
    
                echo "$file";
            }
            closedir($dh);
        }
    }
    ?>
    just need to get rid of
    .
    ..

    thanks
    Hey new design new look, goto xudas for personal webdsign help.. (:

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    ../ indicates the file above the current directory.

    if the include directory is underneath the document root or accessible to the web (http://mydomain.com) you have 2 options. you can use the absolute path (http://mydomain.com/folder/file)
    or a relative absolute path (/folder/file)... the first forward slash is an indication to start at the document root.

    if the include file is above the document root well it would be more tricky. because you need to edit some other scripts on your server. we can tackle that if this is the case... PS we would need the OS Type (Linux / Unix / Windows)

  3. #3
    Join Date
    Oct 2005
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I think its windows...
    Hey new design new look, goto xudas for personal webdsign help.. (:

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    if (is_dir($dir))
      if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false)
          if (strpos($file, '.') !== 0)
            echo $file;
        closedir($dh);
      }
    }
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Though that's the most efficient in this case, you should learn to make a habit of including this line:
    if ($f=='.'||$f== '..') { continue; }
    (at that start of the loop)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The equivalent would be:
    Code:
    if (is_dir($dir))
      if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
          if (strpos($file, '.') === 0) continue;
          echo $file;
        }
        closedir($dh);
      }
    djr33, you even include braces when you neglect indentation? :-\
    Last edited by Twey; 11-01-2007 at 10:41 PM. Reason: Left an extra brace in.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't feel like wasting space with indentation, as it's a simple and notable command-- skip . and .., not some complex if and reaction.
    I don't like omitting brackets because it guides the eye-- if they aren't there, your eye may continue to find the close bracket (as the first, or, rather, lack thereof, may not be noticed when skimming because it sorta blends with the close ')').

    By the way, I wasn't rebutting your reply, but just telling insanemonkey to use that. It's a good habit, unless you want to completely skip all directories.
    In most cases, one wants to go through everything and of course skip the . and .. directories, but do something with files and do something else with directories; but skip the . and .. dirs. This will end with some weird errors, too, so it's just a basic part of the setup.

    The exception, obviously, is if you just want to skip all directories; in that case, just use the above line.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #8
    Join Date
    Oct 2005
    Posts
    255
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    thankyou you forgot to put some '{' in there but i got it thankyou..
    Hey new design new look, goto xudas for personal webdsign help.. (:

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't feel like wasting space with indentation, as it's a simple and notable command-- skip . and .., not some complex if and reaction.
    Fair.
    I don't like omitting brackets because it guides the eye-- if they aren't there, your eye may continue to find the close bracket (as the first, or, rather, lack thereof, may not be noticed when skimming because it sorta blends with the close ')').
    Not at all. The indentation (or lack thereof) shows clearly that the conditionally-executed block doesn't extend to the next line.
    The exception, obviously, is if you just want to skip all directories; in that case, just use the above line.
    What? My code, at least, had nothing to do with skipping all directories...
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Huh?
    if (strpos($file, '.') === 0) continue;
    What else DOES that do?


    As for indentation, that's fair enough, but I find the brackets still helpful if skimming quickly (as if to find a section of the code). When looking at each line, lack of brackets is fine.
    Also, the real reason for including it is to be absolutely correct. I see omitting the brackets as a shortcut, just like omitting the ; in javascript. I prefer having these, just to be sure that's not the problem if/when debugging. If I'm writing quickly, I may omit them. (Or to minimize the characters in a code, if there's some reason.)
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •