Reading content of a directory

Hello,

I’m useing a script (which is close to the Example 499 at http://de3.php.net/manual/en/function.readdir.php) to check for files in a directory.
For some reason it takes always a little to much time to list those files.

My script is following:



$handle = opendir ('/path_to_dir/');
 
$content .= "<b>files in directory:</b><br />";
while (false !== ($file = readdir ($handle))) {
if ($file != "" && $file != ".." && $file != "." && $file != "some_dir") {
$delfile = "$file";

$content .= "$file - <span class=\\"alert\\"><a href=\\"delete.php?file=$delfile\\">[delete]</a></span><br />";
$filecount_ul = "1";
} 
}
closedir($handle);
 
if($filecount_ul != "1") {
$content .= "no files";

My questions:

1.) What can I do to speed this up. (There are 5 - max. about 30 files to be listed…)

2.) It looks like the information is not cached, thus even when reloading, it again takes time. Could I somehow cache this information for 5 minutes or better until want it to be updated?

thanx
Flözen

Use glob():

$files = glob('path-to-dir/*.*');
foreach ($files as $file) {
  if (is_dir($file)) continue;
  // do stuff
}

$files will be an array of strings representing the paths to files (and other directories) in the dir. You can save this array somehow and use it later on. If you want to filter things out (like directories) you can splice them out of the array or create a new one, adding the things that pass the filter to it upon each iteration of the loop.

I would store the files array in a cookie.

Why? Some people block cookies, not a reliable method at all. Also, it means you’re giving away to the public the contents of your directory, which is probably a security issue.

Better to store it in a session variable or a text file in the working directory.

Do I understand this right: You would store the content of the array in a file. Only if this stored content is older than for example 5 Minutes, your would run the array again?

Sounds good in my opinion. :slight_smile:

But anyway, why isn’t there a faster way to show the content of a directory? Listing it in a shell takes not even milliseconds…?!

Thanks,
Flözen

Although this topic is over two years old, I still have this problem - even on a new machine.

Since I worked a little on caching technics it’s working better. But for the first call of this functtion, it takes for ever to parse the site.

This function is only used for administrative reasons. But if you log into this area, this readdir() function seams to be responible for a 20-40 second parse-time.
Since directory listings in the shell take milliseconds, I wonder, what PHP does?!

No suggestions?!

Sounds really odd, but I can think of possibilities. First things first - make sure it really takes a long time. Time it.


microtime(true);
$start = microtime(true);



$handle = opendir ('/path_to_dir/'); 

$content .= "<b>files in directory:</b><br />"; 
while (false !== ($file = readdir ($handle))) { 
if ($file != "" && $file != ".." && $file != "." && $file != "some_dir") { 
$delfile = "$file"; 

$content .= "$file - <span class=\\"alert\\"><a href=\\"delete.php?file=$delfile\\">[delete]</a></span><br />"; 
$filecount_ul = "1"; 
} 
} 
closedir($handle);

$end = microtime(true);
printf('&#37;.5f', $end - $start);