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

Thread: opendir?

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default opendir?

    This is only echoing one of my directories as a link. It's echos them all before the "if (is_dir)" and after the while (so between line 3 echo $file; would be). The only directory that echos is the only one that has contents, so does the directory have to have contents for the is_dir to be true? Thanks.

    PHP Code:
    <?php
    if ($handle opendir('./images')) {
        while (
    false !== ($file readdir($handle))) {
            if (
    is_dir($file)) {
                if (
    substr("$file"01) != ".")  {
    ?> 
                <a href="<?php echo $file?>"><?php echo $file?></a><br />
    <?php
                
    }
            }
        
    $value++;
        }
        
    closedir($handle);
    }
    ?>
    Last edited by bluewalrus; 12-27-2009 at 07:11 PM.

  2. #2
    Join Date
    Jul 2008
    Posts
    199
    Thanks
    6
    Thanked 58 Times in 57 Posts

    Default

    This is probability a better solution for what you're trying to do, just make sure you add in the file extensions you wish to display:
    PHP Code:
    $files glob('./images/*.{jpg,png,gif}'GLOB_BRACE);

    $value 0;

    foreach(
    $files as $file)
    {
        echo 
    '<a href="' $file '">' $file '</a><br />';
        
    $value++;


  3. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I don't want to display any files only directories.

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

    Default

    This line is causing the problems:
    PHP Code:
                if (substr("$file"01) != ".")  { 
    1. Why did you use quotes? It should be $file, not "$file"

    2. This says "if not directory", which is a contradiction to "if is_dir()".
    "." is the first character of EVERY directory. "." is the current directory and ".." is one directory "up" in the filesystem. But other directories have a dot first too, like the directory "example" within the current directory is actually seen as ".example" when searching like this.
    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

  5. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    It use to be text never took the time to change when changed to variable, it still parses the same though right? That pulls out the .. and . from the listings of the directories, as well as any files that's first character is a . because they are suppose to be hidden.

    Why does it say "if not directory" shouldn't/doesn't it say

    PHP Code:
    if ($handle opendir('./images')) { //open images folder
        
    while (false !== ($file readdir($handle))) { //read images folder
            
    if (is_dir($file)) { //if file is a directory
                
    if (substr("$file"01) != ".")  { //if it's first character is not a "." ....next line echos a link to it because it is a directory. 
    This all works I get the directory to display and it hides all other files, but it only displays 1 of 8 directories the other 7 directories though do not have files in them so does the is_dir require the directory have contents? Thanks.

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

    Default

    Isn't the "." for all directories? Maybe I'm just very wrong then. If it's hidden files, sorry for a confusing answer. I meant that searching for a first character dot was searching for directories.

    But again, why are you skipping the dot ones? I would use this line instead of that:
    if ($file!='.'&&$file!='..') {

    That should accomplish the same thing. See if that helps.
    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

  7. #7
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Still the same result, only the 1 directory listed.
    I'm not sure on the "." and ".." I forget what they call back to right now.

    That line wouldn't do the same though for all instances of directory reading, for example ".hidethisimage.jpg" would be displayed by that line. The . at the start of a file name on macs hides them not sure about windows or on the server if that is true without the line used above.
    Last edited by bluewalrus; 12-26-2009 at 07:25 AM.

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

    Default

    Yes, I understand. I'm not exactly sure, then.

    Nothing jumps out at me in your code. However, try changing this line:
    if ($handle = opendir('./images')) {

    './images' is the same thing as just 'images'

    I can't see anything else at the moment. If that doesn't work, I'll try to just rewrite it all tomorrow. It's not that much code
    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

  9. The Following User Says Thank You to djr33 For This Useful Post:

    bluewalrus (12-26-2009)

  10. #9
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I forgot about that value++ variable that was to debug.

    So this is my code now:
    PHP Code:
    <?php
    if ($handle opendir('./images')) {
        while (
    false !== ($file readdir($handle))) {
            if (
    is_dir($file)) {
                 if (
    substr($file01) != ".")  {
    ?> 
                <a href="<?php echo $file?>"><?php echo $file?></a><br />
    <?php
                
    }
            }
        }
        
    closedir($handle);
    }
    This is the output from this:
    HTML Code:
    <a href="fraser">fraser</a><br />
    My debug code is:

    PHP Code:
    <?php
    $value 
    0;
    if (
    $handle opendir('./images')) {
        while (
    false !== ($file readdir($handle))) {
        echo 
    "#" $value "&nbsp;" $file "<br />\n";
            if (
    is_dir($file)) {
                 if (
    substr($file01) != ".")  {
    ?> 
                <a href="<?php echo $file?>"><?php echo $file?></a><br />
    <?php
                
    }
            }
            
    $value++;
        }
        
    closedir($handle);
    }
    ?>
    and the output is

    HTML Code:
    #0&nbsp;fraser<br />
     
    			<a href="fraser">fraser</a><br />
    #1&nbsp;icons<br />
    #2&nbsp;hogan<br />
    
    #3&nbsp;admin<br />
    #4&nbsp;..<br />
    #5&nbsp;.<br />
    #6&nbsp;original<br />
    #7&nbsp;loading.gif<br />
    #8&nbsp;chris<br />
    I also tried adding a file into the hogan directory but that didn't get it to show up so it's not that the direcotry needs content.

  11. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I figured it out. You need to supply the path to the is_dir() method, example:

    Code:
    <?php
    $pth = './images/';
    if ($handle = opendir($pth)) { 
        while (false !== ($file = readdir($handle))) { 
            if (is_dir($pth . $file)) { 
                if (substr($file, 0, 1) !== ".")  { 
    ?>  
                <a href="<?php echo $pth . $file; ?>"><?php echo $file; ?></a><br /> 
    <?php 
                } 
            } 
        } 
        closedir($handle); 
    } 
    ?>
    Without that, the only directories that will pass is_dir() (be included in the list) are ones that are directories of both the root folder and of the images folder. So if you had a folder named 'test' in the root, and one named 'test' in the images folder, that one would show up.

    Also note that if you do not include the path in the href (as shown above, href="<?php echo $pth . $file; ?>"), It will either be to the wrong folder, or to no folder.

    It's not required here:

    Code:
    if (substr($file, 0, 1) !== ".")  {
    In fact, including it there would mess up that test.

    Note: The root folder in the above is just where I was running this from. It could be any immediate parent folder of the (in this case images) folder being checked.
    Last edited by jscheuer1; 12-26-2009 at 05:25 PM. Reason: add note about root
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  12. The Following User Says Thank You to jscheuer1 For This Useful Post:

    bluewalrus (12-26-2009)

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
  •