www.webdeveloper.com
Results 1 to 3 of 3

Thread: Complex search of directory contents

  1. #1
    Join Date
    Jun 2005
    Posts
    81

    Question Complex search of directory contents

    I am creating a web page in which I want it to give me the file contents of a directory with a specified prefix. For example, when I go to the page with "quinn" sent in the GET string, I want the page to give me a list of all the images that have quinn at the beginning of the filename. What is the best way to do this? Is there a way to do this with scandir?

    Chris

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,247
    glob() is probably simpler. If you just want the file names, I'd do something like:
    PHP Code:
    $cwd getcwd();
    chdir('path/to/your/images');
    $fileList glob($prefix.'*.jpg');
    chdir($cwd); // back to where we started
    foreach($fileList as $fileName) {
        
    // do something with $fileName

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    862
    [EDIT]
    NogDog is too fast, and better at PHP
    It'd be best to just go with his solution.

    You could achieve something like this through the use of glob(). But first, a word of caution as scripts that deal with the filesystem in PHP can often lead to some serious problems if not properly coded to handle all of the 'bad things' that could go wrong.

    The glob() function will return all of the files (and/or folders) in a given directory that matches a pattern you specify. You could then loop through the returned files and only display ones that match your prefix. Or heck, you could probably just have it directly pull only the files that match, however I'm not familiar enough with glob() to give you that code.

    PHP Code:
    <?php
        $prefix 
    = (isset($_GET['prefix'])) ? $_GET['prefix'] : "";
        
        
    $fList glob("*.jpg"); // You can use glob("*.*") to return all files
        
    $rList = array();
        
        
    // Check for the prefix at the beginning of the filename
        
    for($i 0$i count($fList); $i++) {
            if(
    stripos($fList[$i], $prefix) !== FALSE && stripos($fList[$i], $prefix) == 0$rList[] = $fList[$i];
        }
        
        
    // Echo out the results
        
    for($j 0$j count($rList); $j++) {
            echo 
    $rList[$i] . '<br />';
        }
    ?>
    Last edited by Sup3rkirby; 04-14-2014 at 03:42 PM.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles