Faster read of filenames

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sonnich

    #1

    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($sSearc hPath))
    {
    while (false !== ($file = readdir($handle )))
    {
    $file2 = strtoupper($fil e);
    if( (strpos($file2, $code)!==False) && (strpos($file2,
    ".PDF")!==False ))
    {
    $files[] = $file;
    $paths[] = $sSearchPath;
    }
    }
    closedir($handl e);
    }

  • Jeff North

    #2
    Re: Faster read of filenames

    On 31 Oct 2006 03:22:03 -0800, in comp.lang.php "Sonnich"
    <sonnich.jensen @elektrobit.com >
    <1162293723.742 405.257780@e3g2 000cwe.googlegr oups.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($sSearc hPath))
    >| {
    >| while (false !== ($file = readdir($handle )))
    >| {
    >| $file2 = strtoupper($fil e);
    >| if( (strpos($file2, $code)!==False) && (strpos($file2,
    >| ".PDF")!==False ))
    >| {
    >| $files[] = $file;
    >| $paths[] = $sSearchPath;
    >| }
    >| }
    >| closedir($handl e);
    >| }
    glob() maybe?
    foreach (glob("*.txt") as $filename)
    {
    echo "$filename size " . filesize($filen ame) . "\n";
    }
    ---------------------------------------------------------------
    jnorthau@yourpa ntsyahoo.com.au : Remove your pants to reply
    ---------------------------------------------------------------

    Comment

    • Pedro Graca

      #3
      Re: Faster read of filenames

      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*.

      Comment

      • Sonnich

        #4
        Re: Faster read of filenames


        Jeff North wrote:
        On 31 Oct 2006 03:22:03 -0800, in comp.lang.php "Sonnich"
        <sonnich.jensen @elektrobit.com >
        <1162293723.742 405.257780@e3g2 000cwe.googlegr oups.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($sSearc hPath))
        | {
        | while (false !== ($file = readdir($handle )))
        | {
        | $file2 = strtoupper($fil e);
        | if( (strpos($file2, $code)!==False) && (strpos($file2,
        | ".PDF")!==False ))
        | {
        | $files[] = $file;
        | $paths[] = $sSearchPath;
        | }
        | }
        | closedir($handl e);
        | }
        >
        glob() maybe?
        foreach (glob("*.txt") as $filename)
        {
        echo "$filename size " . filesize($filen ame) . "\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\somewh ere\AB123*.pdf or \\server\somewh ere\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

        Comment

        • NC

          #5
          Re: Faster read of filenames

          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\somewh ere\AB123*.pdf or \\server\somewh ere\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\somewh ere\*123*.pdf
          \\server\somewh ere\??123*.pdf
          \\server\somewh ere\{a,A}{b,B}1 23*.pdf

          Cheers,
          NC

          Comment

          • Sonnich

            #6
            Re: Faster read of filenames

            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

            Comment

            Working...