Jump to content

Archived

This topic is now archived and is closed to further replies.

sneskid

dir functions

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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.