PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
previousdio_writechdirnext
Last updated: Tue, 03 Sep 2002
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese | Czech | Dutch | Finnish | French | German | Hungarian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Swedish | Turkish

XXIV. Funzioni per le directory

Introduzione

Requisiti

Queste funzioni sono disponibili nei moduli standard, che sono sempre disponibili.

Istallazione

Non ` necessaria nessuna installazione per usare queste funzioni, esse fanno parte del core di PHP.

Configurazione Runtime

Questa estensione non definisce alcuna direttiva di configurazione

Resource Type

Costanti Predefinite

Questa estensione non definisce alcuna costante.

Vedere Anche

Per funzioni correlate, quali dirname(), is_dir(), mkdir() e rmdir(), vedere la sezione Filesystem.

Sommario
chdir -- cambia directory
chroot -- cambia la direcory di root
dir -- classe directory
closedir -- chiude l'handle della directory
getcwd -- restituisce la directory di lavoro in uso
opendir -- apre l'handle della directory
readdir -- legge una voce dall'handle della directory
rewinddir -- riavvolge l'handle della directory
User Contributed Notes
Funzioni per le directory
add a note about notes
[email protected]
13-Dec-2000 08:06

List all files in a directory tree under Win32:


function list_dir($dirname)
{
if($dirname[strlen($dirname)-1]!='\\')
$dirname.='\\';
static $result_array=array();
$handle=opendir($dirname);
while ($file = readdir($handle))
{
if($file=='.'||$file=='..')
continue;
if(is_dir($dirname.$file))
list_dir($dirname.$file.'\\');
else
$result_array[]=$dirname.$file;
}
closedir($handle);
return $result_array;

}

avbentem at hetnet.nl
24-Feb-2002 11:06

The user annotation above it not correct: one should really use 'identical' to check for FALSE, as a directory may be named FALSE as well...

Thus:

while ($file = readdir($handle))

should read

while (false !== ($file = readdir($handle)))

See

Arjan.

ben AT tech-space DOT net
23-May-2002 06:10

//lists all files in a directory with a given file name convention

function list_dir($file_name_convention) {
switch ($file_name_convention) {
case 'dated_reports':
//matches files like '5_23_2002.html'
$this_regexp = "/[0-9]{1,}_[0-9]{1,}_[0-9]{4,}/";
break;
//add more cases of file name conventions
}
$this_dir = dir('.');
if ($this_regexp != null) {
while ($file = $this_dir->read()) {
if (preg_match($this_regexp, $file)) {
$result_array[] = $file;
}
}
}
return $result_array;
}

[email protected]
23-May-2002 07:20

Just a side note....not really php'ish

$mstrng = shell_exec('du -sc /usr/local/apache/www');
print "$mstrng";

Will get you the size of a directory, including all of its sub dirs and files within.

tapani
07-Aug-2002 01:54

shell_exec this

ls -la '$dir'|wc -l|sed 's/^ *//;'

to get number of files in dir

david DOT leggett AT ntlworld DOT com
15-Aug-2002 04:27

array filelist (string directory)

This function will recurse all dirs below the specified directory and return with a list of all the files, e.g.
$listoffiles = filelist ("/download");
where /download has the following structure:
/download/file.html
/download/somefolder/anotherfile.tar
/download/somefolder/someotherfolder/afileagain.php
will return

$listoffiles = array (
[0] => "file.html",
[1] => "anotherfile.tar",
[2] => "afileagain.php"
)

I hope you will find this useful.
David Leggett

// File listing function
function filelist ($currentdir, $startdir=NULL, $files=array()) {
chdir ($currentdir);

// remember where we started from
if (!$startdir) {
$startdir = $currentdir;
}
$d = opendir (".");

//list the files in the dir
while ($file = readdir ($d)) {
if ($file != ".." && $file != ".") {
if (is_dir ($file)) {
// If $file is a directory take a look inside
$files = filelist (getcwd().'/'.$file, getcwd(), $files);
} else {
// If $ file is not a directory then add it to our output array
$files[] = $file;
}
}
}

closedir ($d);
chdir ($startdir);
return $files;
}

erere CHIOCCIOLINA iname PUNTO com
17-Aug-2002 01:40

In [email protected]'s function list_dir($dirname):

if(is_dir($dirname.$file))
list_dir($dirname.$file.'\\');

should become:

if(is_dir($dirname.$file))
$result_array[]=list_dir($dirname.$file.'\\');

or recursion will be done, but no subdirs will be pushed into the $result_array.

Ernesto

admin[@T]networkessence.net
21-Aug-2002 12:41

This script will count lines of code that do not begin with // or are not blank space. It's very approximate because it will include /* */ comments and will also include lines that start with blank space and then //. It's a start though (it's recursive, so don't use this with a directory structure that includes symbolic links). This will recursively count all the lines of files that end with .php. You can change .php to whatever, and also change the guidelines as to whether a line qualifies as a comment or not (look around the !="//" part)

$i=0;

function getDirList ($dirName) {
global $i;
$d = dir($dirName);
while($entry = $d->read()) {
if ($entry != "." && $entry != "..") {
if (is_dir($dirName."/".$entry)) {
getDirList($dirName."/".$entry);
} else if(substr($entry, -4)=='.php') {
if($read_file = file($dirName.'/'.$entry))
foreach($read_file as $line)
if(($line!="\n") && (substr($line, 0, 2)!="//"))
$i++;
echo $dirName."/".$entry."\n";
}
}
}
$d->close();
}

getDirList("./path/to/files");

echo $i;

[email protected]
30-Aug-2002 02:10

Note that all directory functions will not work if PHP is running in "safe mode" and the uid of the script is not the uid of the dir owner.
[email protected]
30-Aug-2002 02:36

Small script to list all files + size + date (like the DOS dir command)

//define the path as relative
$path = ".";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

//make string with 70 spaces
$space=" ";

echo "<PRE>";
echo "Directory Listing of $path\n";
//running the while loop

while ($file = readdir($dir_handle)) {

// filename - output left-justified
$t="<a href=$file>$file</a>";
echo $t.substr($space,0,40-strlen($file)) ;

// filesize - output right-justified
$t=(filesize($file)/1024);
$t=sprintf("%01.2f",$t)."kb ";
echo substr($space,0,10-strlen($t)) . $t ;

// filedate - output left-justified
$t=date("d.M Y H:i:s", filemtime($file));
echo $t.substr($space,0,20-strlen($file)) ;
echo "\n";
}
//closing the directory
closedir($dir_handle);
echo "</PRE>";

add a note about notes
previousdio_writechdirnext
Last updated: Tue, 03 Sep 2002
show source | credits | stats | mirror sites
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Fri Sep 6 12:35:50 2002 CEST