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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Help with an open directory script

    What I'm trying to do is come as close to the Apache open directory page as possible, what i have so far is extremely simple, and lists all files and folders in the directory the php file is in. Here goes...

    PHP Code:
    <?

    function getFiles($path) {
       
    //Function takes a path, and returns a numerically indexed array of associative arrays containing file information,
       //sorted by the file name (case insensitive).  If two files are identical when compared without case, they will sort
       //relative to each other in the order presented by readdir()
       
    $files = array();
       
    $fileNames = array();
       
    $i 0;
      
       if (
    is_dir($path)) {
           if (
    $dh opendir($path)) {
               while ((
    $file readdir($dh)) !== false) {
                   if (
    $file == "." || $file == "..") continue;
                   
    $fullpath $path "/" $file;
                   
    $fkey strtolower($file);
                   while (
    array_key_exists($fkey,$fileNames)) $fkey .= " ";
                   
    $a stat($fullpath);
                   
    $files[$fkey]['size'] = $a['size'];
                   if (
    $a['size'] == 0$files[$fkey]['sizetext'] = "-";
                   else if (
    $a['size'] > 1024$files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K";
                   else if (
    $a['size'] > 1024*1024$files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb";
                   else 
    $files[$fkey]['sizetext'] = $a['size'] . " bytes";
                   
    $files[$fkey]['name'] = $file;
                   
    $files[$fkey]['type'] = filetype($fullpath);
                   
    $fileNames[$i++] = $fkey;
               }
               
    closedir($dh);
           } else die (
    "Cannot open directory:  $path");
       } else die (
    "Path is not a directory:  $path");
       
    sort($fileNames,SORT_STRING);
       
    $sortedFiles = array();
       
    $i 0;
       foreach(
    $fileNames as $f$sortedFiles[$i++] = $files[$f];
      
       return 
    $sortedFiles;
    }

    $files getFiles("./");
    foreach (
    $files as $file) print "&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"$file[name]\">$file[name]</a></b><br>\n";
    ?>
    I'd at least like to be able to sort the information by class (file/folder so that folders appear at the top of the list) and create the ability to exclude certain file extensions, such as my *.php and *.html files. This script isn't my work, just something i picked up a while ago, but I decided to try and restart my little project. Any help, even minor improvements on the code would be wonderful, I'm very noobish when it comes to scripting, never could pick up on it in a timely manner...
    Thanks for your help!

  • #2
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Bump

    Recap:
    I'm looking for the ability to exclude *.php and *.html files, and to show folders before files on the list, and any other little tidbits you think would be an improvement!

    Heres what I'm working with www.matts-mods.com/dir.php

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    PHP Code:
    if ($file == "." || $file == ".." || strtolower(substr(strrchr($file'.'), 1)) == 'php' || strtolower(substr(strrchr($file'.'), 1)) == 'html') continue; 
    As for the other problem make another array for folders, use is_dir() with an if/else switch to decide which array to add them to, and before you return it combine the arrays.

  • Users who have thanked Inigoesdr for this post:

    beasters (08-31-2007)

  • #4
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    As for the other problem make another array for folders, use is_dir() with an if/else switch to decide which array to add them to, and before you return it combine the arrays.
    can someone do this for me? I'm not quite knowledgeable enough :-/

  • #5
    New to the CF scene
    Join Date
    Aug 2007
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    bump, can anyone "make another array for folders, use is_dir() with an if/else switch to decide which array to add them to, and before you return it combine the arrays." for me please?

    thanks!

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    This is the general idea:
    PHP Code:
    function getFiles($path)
    {
       
    //Function takes a path, and returns a numerically indexed array of associative arrays containing file information,
       //sorted by the file name (case insensitive).  If two files are identical when compared without case, they will sort
       //relative to each other in the order presented by readdir()
        
    $files $fileNames $dirs = array();
        
    $i $j 0;
      
        if (
    is_dir($path))
        {
            if (
    $dh opendir($path))
            {
                while ((
    $file readdir($dh)) !== false)
                {
                    if (
    $file == "." || $file == ".." || strtolower(substr(strrchr($file'.'), 1)) == 'php' || strtolower(substr(strrchr($file'.'), 1)) == 'html') continue;  
                    
    $fullpath $path "/" $file;
                    
    $fkey strtolower($file);
                    if(
    is_dir($fullpath))
                    {
                        
    $dirs[$j++] = $fkey;
                    }
                    else
                    {
                        while (
    array_key_exists($fkey,$fileNames))
                            
    $fkey .= " ";
                        
    $a stat($fullpath);
                        
    $files[$fkey]['size'] = $a['size'];
                        if (
    $a['size'] == 0)
                            
    $files[$fkey]['sizetext'] = "-";
                        elseif (
    $a['size'] > 1024)
                            
    $files[$fkey]['sizetext'] = (ceil($a['size']/1024*100)/100) . " K";
                        elseif (
    $a['size'] > 1024*1024)
                            
    $files[$fkey]['sizetext'] = (ceil($a['size']/(1024*1024)*100)/100) . " Mb";
                        else
                            
    $files[$fkey]['sizetext'] = $a['size'] . " bytes";
                        
    $files[$fkey]['name'] = $file;
                        
    $files[$fkey]['type'] = filetype($fullpath);
                        
    $fileNames[$i++] = $fkey;
                    }
                }
                
    closedir($dh);
            }
            else
                die (
    "Cannot open directory:  $path");
        }
        else
            die (
    "Path is not a directory:  $path");

        
    sort($fileNames,SORT_STRING);
        
    $sortedFiles = array();
        
    $i 0;
        foreach(
    $fileNames as $f)
            
    $sortedFiles[$i++] = $files[$f];
        
        
    sort($dirsSORT_STRING);
        return array(
    'dirs' => $dirs'files' => $sortedFiles);
    }

    $files getFiles("./");
    echo 
    'Dirs:<br />';
    foreach (
    $files['dirs'] as $dir) print "&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"$dir\">$dir</a></b><br>\n";
    echo 
    '<br /><br />Files:<br />';
    foreach (
    $files['files'] as $file) print "&nbsp;&nbsp;&nbsp;&nbsp;<b><a href=\"$file[name]\">$file[name]</a></b><br>\n"
    You'll have to make changes, since this is only an example.


  •  

    Posting Permissions

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