Solved

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

Posted on 2008-06-20
4
198 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

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…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

737 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