Jump to content

dir functions


sneskid

Recommended Posts

I've read all the typical mumbo jumbo on directory scanning and counting content, but it seems like the more practical stuff is missing.

1: The rewinddir function is nice... but is there a fast way to arbitrarily jump dir position? (like what fseek is to files)
Would this somehow be possible with the resource context param?
   
2: Shouldn't the Count of contents in a directry be kept as a value by the system? Would save time otherwise wasted on iterating through.

3: Hypothetically, if there are millions of files in a directory, are you screwed?

Thanks for any help.
Link to comment
Share on other sites

Here are the answers as far as I know them

1) If any such function exists, I don't know of it. Just define one.
[code]
<?php
function dirseek($path,$num) {
if (!($handle = opendir($path))) {
echo "The path could not be opened";
return;
} else {
$i=0;
while (FALSE !== ($file = readdir($handle)) && $i < $num) {
      $i++;
  }
closedir($handle);
return $file;
}
}
?>
[/code]

2) Whether or not this should be true hypothetically, it's not.

3) No, you underestimate how fast a computer, especially a server can find files.
Link to comment
Share on other sites

my concern with the solution you presented was the needless memory usage it comes with (although it's better than scandir, according to this http://bugs.php.net/bug.php?id=31515)

Thanks though.
I'll have to devise some sort of indexing scheme or something.
Link to comment
Share on other sites

Unless you are running these script on a pretty out of date server, or you are getting a ton of hits I don't think that you need to worry too much about that.
Time the function; I would bet it takes less than a second. And afterwards, the memory no longer being used. Not a particularly heavy burden on any server.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.