468,556 Members | 2,410 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,556 developers. It's quick & easy.

Faster read of filenames

Hi all

I use the opendir and readdir to search for files... but they read in
all files, so sometimes I get several hundreds.
If there is a way to give a wildcard, it might be a lot faster.
Since I do this search 100+ times at certain times, this is the thing
which slows me down.
Disableing this part speeds my code up by 90%.

Any ideas?

Sonnich
if($handle = opendir($sSearchPath))
{
while (false !== ($file = readdir($handle)))
{
$file2 = strtoupper($file);
if( (strpos($file2, $code)!==False) && (strpos($file2,
".PDF")!==False))
{
$files[] = $file;
$paths[] = $sSearchPath;
}
}
closedir($handle);
}

Oct 31 '06 #1
5 1181
On 31 Oct 2006 03:22:03 -0800, in comp.lang.php "Sonnich"
<so************@elektrobit.com>
<11**********************@e3g2000cwe.googlegroups. comwrote:
>| Hi all
|
| I use the opendir and readdir to search for files... but they read in
| all files, so sometimes I get several hundreds.
| If there is a way to give a wildcard, it might be a lot faster.
| Since I do this search 100+ times at certain times, this is the thing
| which slows me down.
| Disableing this part speeds my code up by 90%.
|
| Any ideas?
|
| Sonnich
|
|
| if($handle = opendir($sSearchPath))
| {
| while (false !== ($file = readdir($handle)))
| {
| $file2 = strtoupper($file);
| if( (strpos($file2, $code)!==False) && (strpos($file2,
| ".PDF")!==False))
| {
| $files[] = $file;
| $paths[] = $sSearchPath;
| }
| }
| closedir($handle);
| }
glob() maybe?
foreach (glob("*.txt") as $filename)
{
echo "$filename size " . filesize($filename) . "\n";
}
---------------------------------------------------------------
jn******@yourpantsyahoo.com.au : Remove your pants to reply
---------------------------------------------------------------
Oct 31 '06 #2
Sonnich wrote:
I use the opendir and readdir to search for files... but they read in
all files, so sometimes I get several hundreds.
If there is a way to give a wildcard, it might be a lot faster.
Since I do this search 100+ times at certain times, this is the thing
which slows me down.
Disableing this part speeds my code up by 90%.

Any ideas?
glob()? http://php.net/glob

If your files do not change very often, it may be better to have a
file (or database) with the current files and use that instead of
searching in the file system.

<?php
$filelist = file($path . '/filelist.txt');
foreach ($filelist as $fname) {
$files[] = trim($fname);
$paths[] = $path;
}
?>

Of course you'd have to keep filelist.txt updated.

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Oct 31 '06 #3

Jeff North wrote:
On 31 Oct 2006 03:22:03 -0800, in comp.lang.php "Sonnich"
<so************@elektrobit.com>
<11**********************@e3g2000cwe.googlegroups. comwrote:
| Hi all
|
| I use the opendir and readdir to search for files... but they read in
| all files, so sometimes I get several hundreds.
| If there is a way to give a wildcard, it might be a lot faster.
| Since I do this search 100+ times at certain times, this is the thing
| which slows me down.
| Disableing this part speeds my code up by 90%.
|
| Any ideas?
|
| Sonnich
|
|
| if($handle = opendir($sSearchPath))
| {
| while (false !== ($file = readdir($handle)))
| {
| $file2 = strtoupper($file);
| if( (strpos($file2, $code)!==False) && (strpos($file2,
| ".PDF")!==False))
| {
| $files[] = $file;
| $paths[] = $sSearchPath;
| }
| }
| closedir($handle);
| }

glob() maybe?
foreach (glob("*.txt") as $filename)
{
echo "$filename size " . filesize($filename) . "\n";
}

That might do it, but it is case sensitive. And when trying something
like:

foreach (glob($search, GLOB_BRACE ) as $filename)
{
echo "$filename <br>";
}

I get absolutlely nothing. My search paths are something like:
\\server\somewhere\AB123*.pdf or \\server\somewhere\AB123*.PDF
Both returns a number of files when runned alone, but adding them
togther
$sx = "{".$s1.",".$s2."}";
does not do the trick.

BR
Sonnich

Oct 31 '06 #4
NC
Sonnich wrote:
Jeff North wrote:

glob() maybe?

That might do it, but it is case sensitive.
But of course it is. On Unix, file names are case-sensitive...
And when trying something like:

foreach (glob($search, GLOB_BRACE ) as $filename)
{
echo "$filename <br>";
}

I get absolutlely nothing. My search paths are something like:
\\server\somewhere\AB123*.pdf or \\server\somewhere\AB123*.PDF
Both returns a number of files when runned alone, but adding them
togther
$sx = "{".$s1.",".$s2."}";
does not do the trick.
Try one or more of these:

\\server\somewhere\*123*.pdf
\\server\somewhere\??123*.pdf
\\server\somewhere\{a,A}{b,B}123*.pdf

Cheers,
NC

Oct 31 '06 #5
After some heavy testing I found that opendir/readdir is app twice as
fast as glob, just as mentioned on www.php.net.
So my advice would be to use opendir/readdir in stead of glob.

BR
Sonnich

Oct 31 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by hokiegal99 | last post: by
3 posts views Thread by andrea.gavana | last post: by
2 posts views Thread by rbutch | last post: by
3 posts views Thread by Torsten Bronger | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.