|
Performing a recursive directory search in PHP.
|
|
06-29-2007, 12:26 PM
Post: #1
|
|||
|
|||
|
Performing a recursive directory search in PHP.
Because I'm an object oriented kind of guy, I'll show you how to not only make a recursive directory search but how to make an object that does it for you upon creation. This tutorial requires PHP4/5 and that is all; surprised?
All right, so lets create an initial framework. My explanations will be in the comments, and will describe the usage (or recommendation that they not be used outside the script) and then we'll get into the guts of the script. PHP Code: <?phpNow that means that you can simply call your class like this: PHP Code: $search = new RecursiveSearch(dirname(__FILE__)."/inc/plugins");Now, back to the object on a per-function basis, and making it function like a real wonder. Our constructor really needs some serious work; without initializing variables and starting our search, there's just no way for our object to work. So step one is to initialize the variables. Remember, a class/object variable is referenced inside the object's code as [iphp]$this->varname;[/iphp], so all initialization must use this convention and all internal function calls too. PHP Code: // This is our framework constructor. All we need passed to it is the root directory toNow, before we tackle the __search function lets fix up the __isdot function because it is very important. PHP Code: // I won't explain why this is here yet, but it is crucial if you don't like infinite loops.Quote:Microsoft Windows XP [Version 5.1.2600]Sometimes people ignore these conventions, but . is the same directory as the current directory, and .. is the one below your current location. So if we recursively search without looking for these we get one of two problems: 1. We search the same directory infinitely. 2. We search both downwards and upwards through the entire computer's structure. Yikes! Think about it for a second. You are searching in C:\Temp\htdocs, so the first result is .. Well, lets say you skip it and go to the second, .., dot result? Because of how we will prioritize things you will search C:\Temp long before you ever so much as touch a given file in C:\Temp\htdocs! And, worse still, you would then search not only the root directory/drive, but all of its given pathways in all directions. Ugly? Yeah. That's why if your script ever locks up or starts turning out pages upon pages of irrelevant results your first check should be if you called __isdot(), and then if you handled it right. Enough doom and gloom, lets move onto the real meat and potatoes of the search: __search($dir)! PHP Code: // This is our prototype internal search function. You shouldn't call this on your own. TheDone. But there's gotta be more to it! Really? Recursive searches are deceptively simple, and before you fool yourself make sure that you know, and I mean know, that there really isn't more to it. Sure, you could have a PHP Code: $this->count = count($this->files)+count($this->folders); Our final sourcecode: PHP Code: <?phpThat's all folks! And yes, you can make an anonymous functions as your callback, but sometimes that isn't what you want. You might want to reference a global object or do something that is messy in a string. In any case, it works for me ... just drop it into your own webserver's root directory (assuming that you delete it immediately afterwards, since you don't want folks knowing all the layout of your files) and try it for yourself.
Men best show their character in trifles, where they are not on their guard. It is in the simplest habits, that we often see the boundless egotism which pays no regard to the feelings of others and denies nothing to itself. -- Arthur Schopenhauer
Feed Me!
|
|||
|
11-14-2007, 06:17 AM
Post: #2
|
|||
|
|||
|
RE: Performing a recursive directory search in PHP.
I've tried this class and it works prety good.
The only problem is, I'm not that kind of programmer to know how it really works and yet having a slight problem to fix. I've managed to get the class working on a folder-structure containing over 200.000 entries. They all go into the arrays with the scandir function. Basically I end up reading that array returned from this class in my script performing another search in this array finding specific matches of the searchword entered on the webpage. If I was able to perform that searchaction inside the class, it would only fill the array with scandir's result matching my searchword. Makes it efficient and will spare me from time-out problems. How would I integrate this into this class properly? Erik |
|||
|
11-16-2007, 04:56 PM
(This post was last modified: 11-16-2007 04:56 PM by The Wicked Flea.)
Post: #3
|
|||
|
|||
|
RE: Performing a recursive directory search in PHP.
Hello Erik, thanks for the question. Sorry for the delay in responding, but as you can tell it isn't all that active around here.
My recommendation to you is that you derive a new class based off mine, and then override the __search() function. I would suggest that you add a keyword property to the constructor, as a property, and then change the code for including files into the results part of the __search() function like so: Code: <?phpYou can paste this code below the RecursiveSearch class and use it directly. Unless I've made a typo, this should work though I haven't tested it. Let me know of any trouble.
Men best show their character in trifles, where they are not on their guard. It is in the simplest habits, that we often see the boundless egotism which pays no regard to the feelings of others and denies nothing to itself. -- Arthur Schopenhauer
Feed Me!
|
|||
|
11-17-2007, 03:35 PM
Post: #4
|
|||
|
|||
|
RE: Performing a recursive directory search in PHP.
Awesome!
Now I see what enheriting means using classes! Thanks for the reply. |
|||
|
11-17-2007, 04:56 PM
Post: #5
|
|||
|
|||
|
RE: Performing a recursive directory search in PHP.
You're very welcome.
Inheritance is an extremely powerful side of programming, isn't it?
Men best show their character in trifles, where they are not on their guard. It is in the simplest habits, that we often see the boundless egotism which pays no regard to the feelings of others and denies nothing to itself. -- Arthur Schopenhauer
Feed Me!
|
|||
|
06-24-2008, 06:57 AM
Post: #6
|
|||
|
|||
|
RE: Performing a recursive directory search in PHP.
... What?
Now I feel stupid..... ![]() ^^made by DS.V.L brucklorace Feed Me!"Either you're part of the problem or you're part of the solution or you're just part of the landscape." - Sam from the movie Ronin |
|||
|
06-24-2008, 11:15 AM
Post: #7
|
|||
|
|||
|
RE: Performing a recursive directory search in PHP.
You're not stupid, you just don't know anything about programming is all.
This is a tutorial on how you search directories recursively, meaning that you search subdirectories too, and it presumes prior programming knowledge in PHP.
Men best show their character in trifles, where they are not on their guard. It is in the simplest habits, that we often see the boundless egotism which pays no regard to the feelings of others and denies nothing to itself. -- Arthur Schopenhauer
Feed Me!
|
|||
|
« Next Oldest | Next Newest »
|



![[Image: 214053.png]](http://valenth.com/lab/214053.png)



![[Image: sigfinalresizecopyza7.jpg]](http://img88.imageshack.us/img88/341/sigfinalresizecopyza7.jpg)
Feed Me!
This is a tutorial on how you search directories recursively, meaning that you search subdirectories too, and it presumes prior programming knowledge in PHP.