For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > May 2006 > recursive file llisting









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author recursive file llisting
Chris Styles

2006-05-03, 6:58 pm

Hi All,

I'm fairly new to PHP, so I am a little lost at what to try next.

I am trying to write a function that I can call recursively to descend down
a directory tree, each time it finds a file it will do some processing.

The directory tree I am testing with is below, the script looks in it's own
directory and below :

dirtest/index.php
dirtest/root.c
dirtest/root.h
dirtest/proj1
dirtest/proj1/proj1.c
dirtest/proj1/proj1.h
dirtest/proj1/proj2
dirtest/proj1/proj2/proj2.h
dirtest/proj1/proj2/proj2.c
dirtest/proj1/wibble
dirtest/proj1/wibble/wibble.c
dirtest/proj3
dirtest/proj3/proj3.c
dirtest/proj3/proj4
dirtest/proj3/proj4/proj4.c


Anyway, when run the following script, it does the following

It descends into proj1, and lists the files, but it lists proj2 as a file,
and doesnt recurse.

Likewise for proj1/wibble

Any suggestions?

Cheers

Chris




================================
Script
================================

<?php

$startdir = "/usr/local/apache2/htdocs/dirtest";


function ListDir ( $dirpath )
{

# open the directory
$dh = opendir ($dirpath);

# loop through all the entries
while ( ! ( ( $file = readdir ( $dh ) ) === false ) )
{
print "$dirpath"."/"."$file<br>\n";

# if it is a directory
if ( is_dir($file) )
{

# recurse into this dir
if ( ! ( ($file == "..") or ($file == ".") ) )
{
$newpath = "$dirpath" . "/" . "$file";
print "Recurse into $newpath<br>\n";
ListDir($newpath);
}
}

}

# close the handle
closedir ( $dh );
}

ListDir($startdir);

?>


Gleep

2006-05-03, 9:57 pm

i have some functions you can experiment with...



<?php
//------->function read & list start
function read_dir ($root ,$filter ) {
$root_dir =opendir ($root );
while ( $file_file =readdir ($root_dir )) {
if ( is_dir ($file_file ) && $file_file != "." && $file_file != ".." && substr ($file_file
,0,strlen ($filter )) == $filter ) {
$mtime = filemtime($file_file);
$array = array ( "_" => " " ,$filter => "" );
$thefile =strtr ($file_file ,$array );
echo "<a href=\"" .$file_file ."\" title=\"last modified on ".date("D d M Y g:i A",
$mtime)."\">" .$thefile ."</a>\n" ;
echo "<br>\n" ;
}
}
}
//------->function read & list end
$user =getenv ("REMOTE_ADDR" );
$server =getenv ("SERVER_ADDR" );

if ( $user != $server ) {
read_dir ("." ,"pub_" );
} else {
read_dir ("." ,"priv_" );
}
?>











function dirtree($dir) {
// fr : Lecture du repertoire
// en : Reading the dir
$rep = opendir($dir);
while($file = readdir($rep)) {
// $addr
$addr = ($dir == '.') ? $file : $dir.'/'.$file;

// fr : Si c'est un dossier on l'ouvre
// en : If the "url" is a dir, the script opens it
if(is_dir($addr) && $file != '.' && $file != '..') {
echo '<li>'.$addr.'<ul>'; // You can delete this line

// fr : Lecture du dossier en question
// en : Read the dir
dirtree($addr);

echo '</ul></li>';
}
// fr : Si c'est un fichier
// en : if it's a file
if(is_file($addr)) {
echo '<li>'.$addr.'</li>';
}
}
}
// fr : Exemple
// en : Example
echo '<ul>';
dirtree('D:/');
echo '</ul>';
?>












Description
This function browse all subdirectories of a given directory and apply a callback function to it.

default.file.php
<?php
function dir_walk($dir, $callback)
{
if (!ereg('/$', $dir)) $dir .= '/';
$d = dir($dir);
while ($entry = $d->read())
{
if ('.' == $entry) { $callback($dir); continue; }

if (is_dir($dir . $entry) && '..' != $entry)
{
dir_walk($dir . $entry, $callback);
}
}
$d->close();
}
?>
















/**
* @return void
* @param array $base_dir
* @desc return recursive list of files and directories
*/
function recrusive_dirlist($base_dir)
{
global $getDirList_alldirs,$getDirList_allfiles
;
function getDirList($base)
{
global $getDirList_alldirs,$getDirList_allfiles
;
if(is_dir($base))
{
$dh = opendir($base);
while (false !== ($dir = readdir($dh)))
{
if (is_dir($base ."/". $dir) && $dir !== '.' && $dir !== '..') //note the change in this
line
{
// $subs = $dir ;
$subbase = $base ."/". $dir;//note the change in this line
$getDirList_alldirs[]=$subbase;
getDirList($subbase);
}
elseif(is_file($base ."/". $dir) && $dir !== '.' && $dir !== '..')//change in this line
too
{
$getDirList_allfiles[]=$base ."/". $dir;//change in this line too
}
}
closedir($dh);
}
}

getDirList($base_dir);
$retval['dirs']=$getDirList_alldirs;
$retval['files']=$getDirList_allfiles;
return $retval;
}




















On Wed, 3 May 2006 22:32:34 +0100, "Chris Styles" <chris.styles@no.spam.arm.com> wrote:

>Hi All,
>
>I'm fairly new to PHP, so I am a little lost at what to try next.
>
>I am trying to write a function that I can call recursively to descend down
>a directory tree, each time it finds a file it will do some processing.
>
>The directory tree I am testing with is below, the script looks in it's own
>directory and below :
>
>dirtest/index.php
>dirtest/root.c
>dirtest/root.h
>dirtest/proj1
>dirtest/proj1/proj1.c
>dirtest/proj1/proj1.h
>dirtest/proj1/proj2
>dirtest/proj1/proj2/proj2.h
>dirtest/proj1/proj2/proj2.c
>dirtest/proj1/wibble
>dirtest/proj1/wibble/wibble.c
>dirtest/proj3
>dirtest/proj3/proj3.c
>dirtest/proj3/proj4
>dirtest/proj3/proj4/proj4.c
>
>
>Anyway, when run the following script, it does the following
>
>It descends into proj1, and lists the files, but it lists proj2 as a file,
>and doesnt recurse.
>
>Likewise for proj1/wibble
>
>Any suggestions?
>
>Cheers
>
>Chris
>
>
>
>
>================================
>Script
>================================
>
><?php
>
>$startdir = "/usr/local/apache2/htdocs/dirtest";
>
>
>function ListDir ( $dirpath )
>{
>
># open the directory
>$dh = opendir ($dirpath);
>
># loop through all the entries
>while ( ! ( ( $file = readdir ( $dh ) ) === false ) )
> {
> print "$dirpath"."/"."$file<br>\n";
>
> # if it is a directory
> if ( is_dir($file) )
> {
>
> # recurse into this dir
> if ( ! ( ($file == "..") or ($file == ".") ) )
> {
> $newpath = "$dirpath" . "/" . "$file";
> print "Recurse into $newpath<br>\n";
> ListDir($newpath);
> }
> }
>
> }
>
># close the handle
>closedir ( $dh );
>}
>
>ListDir($startdir);
>
>?>
>


Michael Steinboeck

2006-05-07, 7:01 pm

Chris wrote:
> I'm fairly new to PHP, so I am a little lost at what to try next.


maybe you are familiar with "the" operating system, so you could try
something like

<?php
passthru("find YOURDIR");
?>

HTH
Michael
Colin McKinnon

2006-05-07, 7:01 pm

Chris Styles wrote:

> Hi All,
>
> I'm fairly new to PHP, so I am a little lost at what to try next.
>
> I am trying to write a function that I can call recursively to descend
> down a directory tree, each time it finds a file it will do some
> processing.
>
> The directory tree I am testing with is below, the script looks in it's
> own directory and below :
>


Code looks fine (next time consider giving a less verbose example - few
people on the list are willing to read too much). I would expect it to work
OK. Maybe it's the permissions on proj2?

C.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com