SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reading a directory

    What function would I use to read the name of all files and their sizes in a certain directory and output that onto a page?

  2. #2
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    if ($handle opendir('/path/to/files')) {
        echo 
    "Files:";
        echo 
    "<table>";

        while (
    false !== ($file readdir($handle))) { 
            
    printf("<tr><td>%s</td><td>%s</td></tr>"$filefilesize($file));
        }

        echo 
    "</table>";
        
    closedir($handle); 
    }
    ?>

  3. #3
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First I got an accessd denied but a chmod edit fixed that.

    Here's are the results though. Not exactly what I want.

    PHP Code:
    Warningstat failed for site_db_backup.sql (errno=No such file or directoryin /home/cgshock/public_html/admincp/backup.php on line 20
     site_db_backup
    .sql  
    .htaccess 69 

    Warning
    stat failed for boards_db_backup.sql (errno=No such file or directoryin /home/cgshock/public_html/admincp/backup.php on line 20
     boards_db_backup
    .sql 

    4096 
    .. 4096 
    site_db_backup
    .sql  
    .htaccess 69 
    boards_db_backup
    .sql 
    Plus I don't want this to list files such as htaccess.

  4. #4
    morphine for a wooden leg randem's Avatar
    Join Date
    Jun 2002
    Location
    .chicago.il.us
    Posts
    957
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops... had one small mistake... ;-)

    PHP Code:
    <?php
    $path 
    '/path/to/files';
    if(
    $dir opendir($path)) {
        echo 
    "Files in $path:";
        echo 
    "<table>";

        while (
    false !== ($file readdir($dir))) { 
            
    printf("<tr><td>%s</td><td>%s</td></tr>"$filefilesize("$path/$file"));
        }

        echo 
    "</table>";
        
    closedir($dir); 
    }
    ?>

  5. #5
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works great, but only one thing. How would I take out the ".", "..", and any files that begin with a dot (such as htaccess) from the results?

    Since I'm planning to store files of only one type in a dir perhaps do some sort of a filter to only display .SQLs?

    Also how can I change the file size to be displayed in kbs instead of bytes?
    Last edited by Codename49; Jun 25, 2002 at 10:37.

  6. #6
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <?php
    $path = '/path/to/files';
    if($dir = opendir($path)) {
        echo "Files in $path:";
        echo "<table>";
    
        while ($file = readdir($dir)) {
            if (!preg_match('/\.\.?$/', $file))
                printf("<tr><td>%s</td><td>%s</td></tr>", $file, filesize("$path/$file"));
        }
    
        echo "</table>";
        closedir($dir); 
    }
    ?>
    the line

    if (!preg_match('/\.\.?$/', $file))

    ignores the "." and ".." entry.

    if you want to list just those files with a specific extension use this line instead of the if (!...)

    Code:
    if (preg_match('/\.(sql|php|inc)$/i', $file))
    You can list other extension inside the brackets, just separate them with a pipe "|".


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
  •