Jump to content


Photo

dir functions


  • Please log in to reply
3 replies to this topic

#1 sneskid

sneskid
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 27 October 2006 - 11:44 PM

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.

#2 ksteuber

ksteuber
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationCalifornia, USA

Posted 28 October 2006 - 02:32 AM

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.
<?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;
}
}
?>

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.


#3 sneskid

sneskid
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 28 October 2006 - 03:55 AM

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.

#4 ksteuber

ksteuber
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationCalifornia, USA

Posted 28 October 2006 - 04:16 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users