Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > some php help?

some php help?
Thread Tools
cnelson87
Forum Regular
Join Date: Jul 2002
Location: Seattle
Status: Offline
Reply With Quote
Mar 25, 2005, 07:47 PM
 
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!)
     
MDWeezer
Forum Regular
Join Date: Feb 2005
Status: Offline
Reply With Quote
Mar 25, 2005, 09:27 PM
 
My buddy wrote a recursive directory print script, it might help you out and give you some insight.

PHP Code
     
cnelson87  (op)
Forum Regular
Join Date: Jul 2002
Location: Seattle
Status: Offline
Reply With Quote
Mar 25, 2005, 09:49 PM
 
I couldn't get that script to work, but thanks for trying. anyways, I'm trying to figure out arrays.
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Mar 28, 2005, 10:59 PM
 
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
     
Simon Mundy
Grizzled Veteran
Join Date: Jun 2001
Location: Melbourne, Australia
Status: Offline
Reply With Quote
Mar 29, 2005, 12:11 AM
 
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...
     
cnelson87  (op)
Forum Regular
Join Date: Jul 2002
Location: Seattle
Status: Offline
Reply With Quote
Mar 29, 2005, 04:58 PM
 
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!
     
the razoor
Fresh-Faced Recruit
Join Date: Apr 2005
Location: Houston
Status: Offline
Reply With Quote
Apr 8, 2005, 10:43 AM
 
I personally like tothe php bible
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:18 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,