SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    displays all files and all directories

    Code:
    <?php 
    $dir = "*.*";
    foreach(glob($dir) as $file)   {echo $file . "<br />";  }   
    ?>
    The code above displays all files in the directory.
    You can see the result of the code above at http://dot.kr/x-test/displayDir/test.php
    .

    But actually the directory "displayDir" has not only some files but aslo some directories(folders).
    I like to display not only all files but also all directories in the directory.

  2. #2
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    514
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    Code:
    <?php 
    $dir = "*.*";
    foreach(glob($dir) as $file)   {echo $file . "<br />";  }   
    ?>
    The code above displays all files in the directory.
    You can see the result of the code above at http://dot.kr/x-test/displayDir/test.php
    .

    But actually the directory "displayDir" has not only some files but aslo some directories(folders).
    I like to display not only all files but also all directories in the directory.

    PHP Code:
    $dir "*.*"
    looks for names with a dot in it. Change it to:

    PHP Code:
    <?php 
    $dir 
    "*";
    foreach(
    glob($dir) as $file)   {echo $file "<br />";  }   
    ?>
    Denny Schlesinger
    web services

  3. #3
    Get my greedy down dotJoon's Avatar
    Join Date
    Apr 2003
    Location
    daejeon, South Korea
    Posts
    2,207
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your code works fine.
    Thank you, captainccs.


    By the way, How can I sort them directories first and files later?

  4. #4
    SitePoint Evangelist captainccs's Avatar
    Join Date
    Mar 2004
    Location
    Caracas, Venezuela
    Posts
    514
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dotJoon View Post
    Your code works fine.
    Thank you, captainccs.


    By the way, How can I sort them directories first and files later?
    Get the in two separate queries. Unfortunately I don't know how to build the regex for directories only:
    PHP Code:
    // get directories
    $dir "/regex goes here/"
    foreach(
    glob($dir) as $directory)   {echo $directory "<br />";  }    

    // get files
    $dir "*.*"
    foreach(
    glob($dir) as $file)   {echo $file "<br />";  } 
    Since I can't get my head around regex, I use the old fashioned no-regex:
    PHP Code:
    $dirs scandir(getcwd());
    echo 
    "Directories:<br />\n";
    $files = array();
    foreach(
    $dirs AS $name) {
        if(
    strpos($name'.') === FALSE) {  // only directories
            
    echo $name "<br />\n";
        } elseif(
    strpos($name'.') !== 0) {  // drop system files
            
    $files[] = $name
        }
    }
    echo 
    "<br />Files:<br />\n";
    foreach(
    $files as $file) {
        echo 
    $file "<br />\n";

    Denny Schlesinger
    web services

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might try...
    PHP Code:
    <?php
    $directory 
    "*";
    $files glob($directory "*");
    foreach(
    $files as $file) { if(is_dir($file)) { $dir_array[] = $file; }else{ $file_array[] = $file; } }
    if(
    count($dir_array)>0) { $i=0; while($i<count($dir_array)) { echo "folder = " $dir_array[$i] . "<br>"$i ++; }}
    echo 
    "<hr>";
    $i=0;
    while(
    $i<count($file_array)){ echo "file = " $file_array[$i] . "<br>"$i ++; }
    ?>


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
  •