Solved

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

Posted on 2008-06-20
4
196 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

821 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