search a file or directory

Discussion in 'PHP' started by gigamike, Aug 2, 2007.

  1. #1
    Guys,

    I hope you can give me an idea. Im trying to create a small script that searches a file or directory. Something like

    $current_directory=$_SERVER['DOCUMENT_ROOT'];
    if(!is_dir($current_directory)){
    if ($handle = opendir($current_directory)){
    while (false !== ($file = readdir($handle))) {
    if(is_file($file)){
    if ($file==trim($search_key)){
    // File found
    }
    }
    }
    }


    Im having trouble if the directory has a directory inside. How i can keep on search the file in all directory

    Thanks,

    Mike
     
    gigamike, Aug 2, 2007 IP
  2. exodus

    exodus Well-Known Member

    Messages:
    1,900
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    165
    #2
    http://us3.php.net/glob

    You should look over the glob fuction. That page has a bunch of file searching routines that use the glob function of php.
     
    exodus, Aug 2, 2007 IP
  3. gigamike

    gigamike Active Member

    Messages:
    165
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Hi exodus,

    Thanks for the quickhelp. Im not sure, but still im having trouble with the logic, coz im planning to look for a file/folder with in a given directory. Exampl i want to look for test file/folder and the structure of my directory is this

    /test
    /folderA/test.txt
    /folderA/folderB/folderC/test.txt


    see what i mean, im stuck with the looping within subdirectory, coz i want to display on where all the search string test in all direcory either its a file or folder

    Thanks,

    Mike

     
    gigamike, Aug 2, 2007 IP