SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Listing files in a directory 10 at a time

    I have the following script for printing the first 10 files in a directory:
    PHP Code:
    <?
    $handle 
    opendir('.'); 

    print    
    "<table>
        <tr bgcolor='#CCCCCC'>
          <td>Filename: </td>
          <td>File-size: </td>
        </tr>"
    ;

    $max '10';
    $min 0;
            
    while (
    false !== ($file readdir($handle))) { 
        if (
    $i >= $min && $i <= $max){
        if (
    $file != "." && $file != ".." && ereg(".php",$file)) { 
            
    $file_size ceil(filesize ($file) / 1024);
                print 
    "<tr>
                    <td><a href='
    $file'>$file</a </td> 
                    <td align='right'>
    $file_size k </td>
                    </tr>"

        }
        
    $i++;
        }
        else {
            die();
        } 
    }
    closedir($handle); 
    ?>
    Which works like a charm. However, what I want is to allow the user to scroll from one page to another when there are more than 10 files in a directory.

    So: How do I change the code so that 10 files are shown at a time, printing a [next] button when there are more, showing the next 10 files?

    Any help is appreciated

    Jazz

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is one I wrote a while back that does what you want.

    PHP Code:
    <html>
    <head>
        <title>Untitled</title>
    </head>

    <body>
    <table>
        <tr bgcolor='#CCCCCC'>
          <td>Filename: </td>
          <td>File-size: </td>
        </tr>
    <?
    $d 
    dir(".");
    $max 10;
    $offset = ($offset) ? $offset 0;

    while(
    $f $d->read()) {
        if((
    $f != '.') && ($f != '..') && !is_dir($f)) {
            
    $files[] = array("name" => $f,
                             
    "size" => filesize($f)
                             );
            }
        }
    $d->close();

    if(
    count($files)) {
        
    $top = (($offset+$max) > count($files)) ? count($files) : $offset $max;
        for(
    $i=$offset;$i<$top;$i++) {
            
    ?>
            <tr>
                <td><?=$files[$i]['name']?></td>
                <td><?=$files[$i]['size']*.001?> kb</td>
            </tr>
            <?
            
    }
        }
    else {
        print 
    "<tr><td colspan=2>No files in this direcotry</td></tr>";
        }

    if(
    $offset 0) {
        
    ?>
            <tr>
                <td colspan=2 align=right><a href="<?=$PHP_SELF?>?offset=<?=($offset $max)?>">[prev]</a></td>
            </tr>
        <?
        
    }
    if((
    count($files) > $max) && (($offset $max) < count($files))) {
        
    ?>
            <tr>
                <td colspan=2 align=right><a href="<?=$PHP_SELF?>?offset=<?=($offset $max)?>">[next]</a></td>
            </tr>
        <?
        
    }    
        

    ?>
    </table>
    </body>
    </html>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Freddy!!

    It works like a charm. Just perfect.

    I have one more, related question:

    $d reads out the current directory. Now if I change it to: dir("./images/"); I get the files from the images-folder.
    But somehow, it won't display the size of the images.

    Is it possible to use the same file (I called it directoryread.php) for different folders, without having to copy the file into the different folders?

    Thanx!

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes you'll just need to set a var for the folde ryou want to to read then use that var in the filesize() function.

    Make these changes

    PHP Code:
    $d dir("."); 
    change to

    PHP Code:
    $dirToRead 'beavis/';
    $d dir($dirToRead); 
    AND


    PHP Code:
                         "size" => filesize($f

    change to

    PHP Code:
                         "size" => filesize($dirToRead.$f
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •