Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

[PHP] paging a directory list

Options
  • 11-03-2007 10:46pm
    #1
    Registered Users Posts: 4,475 ✭✭✭


    I want to show all the files in a certain directory, so I'm using opendir and readdir to output the filenames, but I want to show say only 20 files on a page and then give the user next and previous buttons to show more.

    The only way I can think of doing this is to work out what page I'm on (via a hidden form variable) and then loop through the entire directory, only kicking in the printing of the filename when I reach the appropriate file number.

    So page 1, loop through all files, start printing the filename at file 1 and stop at file 20
    page 2, loop through all files, start printing the filename at file 21 and stop at 40, etc

    This, to my mind is rubbish. Page 10, I'm going to have to skip through 200 files before I start printing. I have no idea what readdir's access time is, but this piece of code is going to be in use by quite a few users at the same time, so I suspect this solution would be slow and cumbersome, but I can't think of a better way to do it.


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Load all the filenames into an array and loop through that.


  • Registered Users Posts: 64 ✭✭Legend_Killer


    your method has the weakness that if a file is deleted then you wont have a 1-20 for example and it might fall over.

    Try looping through, as the man above said, but look up some way of setting a "limit" of 20 for the number of files shown. A google of pagination of a directory should show something up.

    I'd like to help more but my PHP is gone out the window a bit since I started programming ruby.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    corblimey wrote:
    This, to my mind is rubbish. Page 10, I'm going to have to skip through 200 files before I start printing. I have no idea what readdir's access time is, but this piece of code is going to be in use by quite a few users at the same time, so I suspect this solution would be slow and cumbersome, but I can't think of a better way to do it.
    There isn't. Filesystems are not relational databases, so there are limits to how efficient your searches are going to be.
    your method has the weakness that if a file is deleted then you wont have a 1-20 for example and it might fall over.
    It won't as he would be reading only those files that actually exist at any one time. i.e. First request, the script will read the directory and print files 1 - 20. Clicks for the next page (second request) and the script will read the directory again and print files 21 - 40, and so on. If a file is deleted inbetween these two requests, it may cause an offset in the printed results (as it would in a similar database search), but nothing will fall over unless you read the directory only once and store that for all your pages (and that can often be less efficient than reading the directory every time).


  • Registered Users Posts: 4,475 ✭✭✭corblimey


    Load all the filenames into an array and loop through that.
    I think that would only be quicker if I persist the array from page to page, but that suffers from not having the latest "version" of the file listing. I guess the only way to do it is my original plan. Hopefully the users of this system will keep the number of files in this directory to a small number :)


Advertisement