Solved

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

Posted on 2008-06-20
4
193 Views
Last Modified: 2010-04-21
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.
0
Comment
Question by:adrake9
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:hernst42
ID: 21834235
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

0
 
LVL 2

Author Comment

by:adrake9
ID: 21834269
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
0
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 21834328
Yes replace
$searchnumber = $_GET['id'];
with
$searchnumber = $_GET['PropNum'];


$searchnumber = '101';
can be ignored, only used for internal testing
0
 
LVL 2

Author Closing Comment

by:adrake9
ID: 31469299
PERFECT. JUST PERFECT. Thank you. Wish I could grad A++, quick and very helpful!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now