Link to home
Start Free TrialLog in
Avatar of adrake9
adrake9

asked on

Using PHP look through a directory of files and filter by 3-digit number

I need to be able to sort through a list of files that have a naming scheme like this.
example:
102.101_floorplan.pdf
102.105_floorplan.pdf
103.105_floorplan.pdf
etc.,.......

what is the easiest way to search through this list. Using the first 3-digit number, get the file names and output a link for download on my web page. I would need to filter the results so that if there were 10 floorplans for one property or 2 floorplans for another it wouldn't matter. It would just output what was filtered through. Any help is appreciated greatly.
Avatar of hernst42
hernst42
Flag of Germany image

Can go like this. Place the code in the file floor and directory where the pdf are stored and give the search as parameter id to get the list.

http://x/pdf/floors.php?id=101
$searchnumber = '101';
$searchnumber = $_GET['id'];
$dir = '.';
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if (substr($file,0,3) !== $searchnumber) {
                continue;
            }
            echo "<a href=\"$file\">$file</a>\n";
        }
        closedir($dh);
    }
}

Open in new window

Avatar of adrake9
adrake9

ASKER

The variable that I'm passing from page to page is:
PropNum=101 the url looks like this "infosheet.php?PropNum=101"

Can this variable be placed into the $searchnumber variable and the script run from that?

-A
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of adrake9

ASKER

PERFECT. JUST PERFECT. Thank you. Wish I could grad A++, quick and very helpful!