Results 1 to 3 of 3

Thread: Complex search of directory contents

  1. #1
    Join Date
    Jun 2005

    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?


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

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Mar 2005
    Behind you...
    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:
    = (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++) {
    stripos($fList[$i], $prefix) !== FALSE && stripos($fList[$i], $prefix) == 0$rList[] = $fList[$i];
    // Echo out the results
    for($j 0$j count($rList); $j++) {
    $rList[$i] . '<br />';
    Last edited by Sup3rkirby; 04-14-2014 at 04: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