SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Searching for a file within a directory using a wildcard

    Hi,

    Is there a way of searching for a file within a directory....but... heres the hard part, you dont know what the directory is called.

    Is there a way of using a kind of wildcard system?

    Would a viable solution be to list all the get the script to find all the directories on the server, and then use a loop to search in them for the file?

    Thanks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Using the *nix shell
    PHP Code:
    echo $command sprintf('find %s -name %s -type f'escapeshellarg($dir), escapeshellarg($filename));
    $results shell_exec($command); 
    Otherwise see glob() to search for dirs, and then use file_exists() on each dir

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2008
    Posts
    455
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks for the response,
    Is there any chance you could expand on how this works?

    Thanks.

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    find is a unix command.

    $dir is the directory you want find to begin searching in. It will search all directorys recursively starting in this dir. You could set $dir = '/'; if you want it to search the entire filesystem, but hopefully you don't need to search so broadly.

    $results is a string. You can echo it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •