User Name
Password

Go Back   Planetarion Forums > Non Planetarion Discussions > Programming and Discussion
Register FAQ Members List Calendar Arcade Today's Posts

Reply
Thread Tools Display Modes
Unread 26 Jan 2004, 18:45   #1
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Recursive file processing in PHP?

Hi,

I've never really got into handling files in PHP, but now I'd like too. I've set myself a simple task: Have multiple nested directories, each with multiple text files in. Then recurse through each directory getting the contents of each text file (a single word) and put the words into an array.

I've got no trouble reading a single file into an array, but have got stuck when trying to recurse a directory.

Anybody ever done stuff like this before?

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 26 Jan 2004, 21:06   #2
wu_trax
Registered User
 
Join Date: Jan 2003
Posts: 4,290
wu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet societywu_trax is a pillar of this Internet society
Re: Recursive file processing in PHP?

here is some info about directories
youll probably need readdir(), and is_dir() (thats here)
__________________
im not tolerant, i just dont care.
wu_trax is offline   Reply With Quote
Unread 27 Jan 2004, 02:04   #3
meglamaniac
Born Sinful
 
meglamaniac's Avatar
 
Join Date: Nov 2000
Location: Loughborough, UK
Posts: 4,059
meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.meglamaniac has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: Recursive file processing in PHP?

I've had to recurse directories before in PHP.
I made a photo album script so you could just dump photos to a specific folder (and subfolders within that folder) over FTP and then the index script would generate the albums (reprocess images for thumbnails, add alt text etc) on the fly.
I pretty much had to do what wu_trax has suggested.
__________________
Worth dying for. Worth killing for. Worth going to hell for. Amen.
meglamaniac is offline   Reply With Quote
Unread 29 Jan 2004, 03:14   #4
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Re: Recursive file processing in PHP?

Code:
<?php
  function traverse_dir($dir,$suffix='') {
    $ret = array();
    if($hnd = opendir($dir)) {
      while(false !== ($f = readdir($hnd))) {
        if(is_dir($dir.'/'.$f)) {
          if ($f!='.' && $f!='..') {
            $tArr = traverse_dir($dir.'/'.$f);
            foreach($tArr as $k => $v) {
              $ret[$k] = $v;
            }
          }
        }
        elseif(is_readable($dir.'/'.$f) && preg_match("/$suffix$/",$f)) {
          $ret["$dir/$f"] = file_get_contents($dir.'/'.$f);
        }
      }
      closedir($hnd);
    }
    return $ret;
  }

  $words = traverse_dir('/home/tom/wibble','\.wibble');
  print_r($words);
?>
Code:
> $ php traverse.php
Array
(
    [/home/tom/wibble/blah/1/2.wibble] => sdfsfme
    [/home/tom/wibble/blah/2/2.wibble] => sdfs34234fme
    [/home/tom/wibble/1.wibble] => mememe
    [/home/tom/wibble/2.wibble] => memedsfme
)
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 29 Jan 2004, 11:14   #5
Mong
Forever Delayed
 
Join Date: Sep 2000
Location: www.netgamers.org
Posts: 1,475
Mong is on a distinguished road
Re: Recursive file processing in PHP?

MT uses spaces to format his code.

I hereby declare him an outcast!!

M.
__________________
Firefly Oper and General l4m3r - "I Do Stuff"

O2 Rip-off campaign

<vampy> plus i hate people ... i despise humanity as a whole

pablissimo "I'm still geting over the fact you just posted a pic of your own vomit"
Mong is offline   Reply With Quote
Unread 29 Jan 2004, 15:07   #6
MT
/dev/zero
Retired Mod
 
MT's Avatar
 
Join Date: May 2000
Posts: 415
MT is an unknown quantity at this point
Re: Recursive file processing in PHP?

using tabs is just WRONG WRONG WRONG
tr "\t" ' '
__________________
#linux : Home of Genius

<idimmu> ok i was chained to a desk with this oriental dude
MT is offline   Reply With Quote
Unread 29 Jan 2004, 15:18   #7
pablissimo
Henry Kelly
 
pablissimo's Avatar
 
Join Date: Apr 2000
Posts: 7,374
pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.pablissimo has ascended to a higher existance and no longer needs rep points to prove the size of his e-penis.
Re: Recursive file processing in PHP?

My Textpad's set to use two-spaces in place of a tab, what's so evil about that? =(
__________________
You're now playing ketchup
pablissimo is offline   Reply With Quote
Reply



Forum Jump


All times are GMT +1. The time now is 18:18.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2002 - 2018