|
|
some php help?
|
|
|
|
Forum Regular
Join Date: Jul 2002
Location: Seattle
Status:
Offline
|
|
Little by little, in my spare time, I'm slooowly learning php. Mainly by devising simple problems and trying to figure out a solution. Right now I'm trying to get my head around multi-dimensional arrays. And I'm stuck.
Basically, I'm trying to write a script that will index a directory and it's subdirectories. I can do the first level just fine by chdir to a given directory, using opendir and readdir, and storing it's contents in an array. But to go deeper I think I need to use a multi-dimensional array.
Here's some code I wrote that will get all the sub directories in a given directory. To start, I would like to go one level deeper. From there I think I could figure put how to go as deep as I want.
$myArray = array();
chdir(directoryPath);
$searchDir = '.';
$openDir = opendir($searchDir);
while ($myDir = readdir($openDir)) {
if ((is_dir($myDir)) AND (substr($myDir, 0, 1) != '.')) {
$myArray[] = $myDir;
$i++;
}
}
$countMyArray = count($myArray);
(ps - yay! my 100th post!)
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Feb 2005
Status:
Offline
|
|
My buddy wrote a recursive directory print script, it might help you out and give you some insight.
PHP Code
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Jul 2002
Location: Seattle
Status:
Offline
|
|
I couldn't get that script to work, but thanks for trying. anyways, I'm trying to figure out arrays.
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
Arrays are not the way to hold arbitrary-depth objects. You need a tree.
|
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
|
|
|
|
|
|
|
|
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status:
Offline
|
|
Originally posted by cnelson87:
I couldn't get that script to work, but thanks for trying. anyways, I'm trying to figure out arrays.
Whilst Arkham's post is true, it's still a good opportunity for you to work out both the advantages and limitations of arrays with this exercise.
However, the real question is: what do you want to do with the data you gather? It's one thing to gather and store this information, but another to work out how you will manipulate and access this information in a meaningful way.
If you simply wanted to store the number of files in each directory as a count,then aan associative array could work just fine. However, if you wanted to store a number of properties and be able to access your information in an abstracted manner, then building a directory object would be the way to go. But that's for later.
Anway, to immediately answer your question, here's a snip from the PHP Cookbook (which is an extremely good read and very handy) by OReilly:-
Code:
function pc_process_dir($dir_name,$max_depth=10,$depth=0)
{
if ($depth >= $max_depth)
{
// raise error?
return false;
}
$subdirectories = array();
$files = array();
if (is_dir($dir_name) && is_readable($dir_name))
{
$d = dir($dir_name);
while (false !== ($f = $d->read()))
{
if (('.' == $f) || ('..' == $f)) { continue; }
if (is_dir("{$dir_name}/{$f}"))
{
array_push($subdirectories,"{$dir_name}/{$f}");
} else {
array_push($files,"{$dir_name}/{$f}");
}
}
$d->close();
foreach ($subdirectories as $subdirectory)
{
$files = array_merge($files,pc_process_dir($subdirectory, $max_depth, $depth + 1));
}
}
return $files;
}
print_r(pc_process_dir('/your/path',10));
|
Computer thez nohhh...
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Jul 2002
Location: Seattle
Status:
Offline
|
|
Originally posted by Simon Mundy:
Whilst Arkham's post is true, it's still a good opportunity for you to work out both the advantages and limitations of arrays with this exercise.
thanks! that was kind of the point. these little projects I come up with serve no real-world purpose other than to further my own knowledge.
it just seemed like a good idea at the time to write a script and drop it at the root level of a web directory that reads all it's sub-directories, and use it to create an entire DHTML navigation tree, dynamically updating anytime new stuff is added to the site. well, that's the idea, I'm not there yet. maybe it's not the most practical idea but it seems like an interesting endeavor.
I'll give your code a shot. I knew I should have bought the PHP Cookbook instead of the PHP Bible!
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Apr 2005
Location: Houston
Status:
Offline
|
|
I personally like tothe php bible
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|