file_exists() question.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cheesecaker
    New Member
    • Feb 2007
    • 66

    file_exists() question.

    Hi, I need to check if a file with a certain string in its filename exists. How would I do that?
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    #2
    what is your desired output?
    Just show us some code, that will give us a clue.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, Cheesecaker.

      Check out glob().

      Comment

      • cheesecaker
        New Member
        • Feb 2007
        • 66

        #4
        Yeah, I tried glob, but it's very inefficient. So I used a looping readdir();

        Here's the function. Works for my circumstances, don't know about anyone else's:

        [PHP]/** Checks whether a file with the specified string in its name
        * exists in the specified directory.
        * Returns 1 if any matching file is found, 0 if not.
        */

        function file_exists_con taining ($directory, $string) {
        $dirhandle = opendir($direct ory);
        $exists = 0;
        while($file = readdir($dirhan dle)) {
        if (strstr($file, $string)) {
        $exists = 1;
        break;
        }
        }
        return $exists;
        }[/PHP]

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, Cheesecaker.

          You may find scandir() useful here.

          Comment

          Working...