// Function for searching files recursively using a regex.
// $type - array of file types, which will be returned as a result.
// $realpath - if set to true, returns absolute paths instead of relative.
// Warning: regex is applied to the whole path (not only to file name).
<?php
function find_regex($path, $regex, $realpath = false, $type = array('file')) {
$result = array();
$realpath && $path = realpath($path);
if (preg_match($regex, $path) && in_array(@filetype($path), $type)) {
$result[] = $path;
}
if ($dh = @opendir($path)) {
while (($obj = readdir($dh)) !== false) {
if ($obj == '.' || $obj == '..') continue;
$obj = $path . DIRECTORY_SEPARATOR . $obj;
$ftype = @filetype($obj);
if ($ftype !== 'dir') {
in_array($ftype, $type) && preg_match($regex, $obj) && $result[] = $obj;
} else {
$result = array_merge($result, find_regex(&$obj, &$regex, &$realpath, &$type));
}
}
}
return $result;
}
Directory 目录函数
简介
需求
要编译本扩展模块无需外部库文件。
安装
本扩展模块作为 PHP 内核的一部分,无需安装即可使用。
运行时配置
本扩展模块在 php.ini 中未定义任何配置选项。
资源类型
预定义常量
以下常量由本扩展模块定义,因此只有在本扩展模块被编译到 PHP 中,或者在运行时被动态加载后才有效。
Note: PATH_SEPARATOR 是在 PHP 4.3.0-RC2 中引入。
Table of Contents
Directories
eeccho
26-Feb-2008 09:05
26-Feb-2008 09:05
eeccho
26-Feb-2008 09:02
26-Feb-2008 09:02
// Function for searching files recursively using a regex.
// $type - array of file types, which will be returned as a result.
// $realpath - if set to true, returns absolute paths instead of relative.
// Warning: regex is applied to the whole path (not only to file name).
<?php
function find_regex($path, $regex, $realpath = false, $type = array('file')) {
$result = array();
$realpath && $path = realpath($path);
if (preg_match($regex, $path) && in_array(@filetype($path), $type)) {
$result[] = $path;
}
if ($dh = @opendir($path)) {
while (($obj = readdir($dh)) !== false) {
if ($obj == '.' || $obj == '..') continue;
$obj = $path . DIRECTORY_SEPARATOR . $obj;
$ftype = @filetype($obj);
if ($ftype !== 'dir') {
in_array($ftype, $type) && preg_match($regex, $obj) && $result[] = $obj;
} else {
$result = array_merge($result, find_regex(&$obj, &$regex, &$realpath, &$type));
}
}
}
return $result;
}
eeccho
26-Feb-2008 09:01
26-Feb-2008 09:01
// Function for searching files recursively using a regex.
// $type - array of file types, which will be returned as a result.
// $realpath - if set to true, returns absolute paths instead of relative.
// Warning: regex is applied to the whole path (not only to file name).
<?php
function find_regex($path, $regex, $realpath = false, $type = array('file')) {
$result = array();
$realpath && $path = realpath($path);
if (preg_match($regex, $path) && in_array(@filetype($path), $type)) {
$result[] = $path;
}
if ($dh = @opendir($path)) {
while (($obj = readdir($dh)) !== false) {
if ($obj == '.' || $obj == '..') continue;
$obj = $path . DIRECTORY_SEPARATOR . $obj;
$ftype = @filetype($obj);
if ($ftype !== 'dir') {
in_array($ftype, $type) && preg_match($regex, $obj) && $result[] = $obj;
} else {
$result = array_merge($result, find_regex(&$obj, &$regex, &$realpath, &$type));
}
}
}
return $result;
}
billy
19-Feb-2008 05:22
19-Feb-2008 05:22
msh at onliners dot dk's code should have
$dir.'/'.$entry everywhere instead of $dir.$entry
you need your system's directory separator, otherwise you'll get /my/pathfilename instead of correctly /my/path/filename
Jean-Paul Wattiaux
01-Sep-2007 11:08
01-Sep-2007 11:08
Mine works as long as the samba volume is actually mounted. Having it listed in the "My Computer" window doesn't warrant that.
adrian
21-Aug-2007 05:33
21-Aug-2007 05:33
Samba mounts under a Windows environment are not accessible using the mounted drive letter. For instance, if you have drive X: in Windows mounted to //example.local/shared_dir (where example.local is a *nix box running Samba), the following constructs
$dir = "X:\\data\\";
$handle = opendir( $dir );
or
$d = dir( $dir );
will return a warning message "failed to open dir: No error"
On the other hand, using the underlying mapping info works just fine. For example:
$dir = "//example.local/shared_dir/data";
$handle = opendir( $dir );
or
$d = dir( $dir );
Both cases do what they're expected to.
phdwight at yahoo dot com
28-Mar-2007 11:45
28-Mar-2007 11:45
I have posted this same observation in scandir, and found out that it is not limited to scandir alone but to ALL directory functions.
Directory functions DOES NOT currently supports Japanese characters.
dkflbk at nm dot ru
15-Sep-2006 07:29
15-Sep-2006 07:29
I wrote a simple backup script which puts all files in his folder (and all of the sub-folders) in one TAR archive...
(It's classic TAR format not USTAR, so filename and path to it can't be longer then 99 chars)
<?php
/***********************************************************
* Title: Classic-TAR based backup script v0.0.1-dev
**********************************************************/
Class Tar_by_Vladson {
var $tar_file;
var $fp;
function Tar_by_Vladson($tar_file='backup.tar') {
$this->tar_file = $tar_file;
$this->fp = fopen($this->tar_file, "wb");
$tree = $this->build_tree();
$this->process_tree($tree);
fputs($this->fp, pack("a512", ""));
fclose($this->fp);
}
function build_tree($dir='.'){
$handle = opendir($dir);
while(false !== ($readdir = readdir($handle))){
if($readdir != '.' && $readdir != '..'){
$path = $dir.'/'.$readdir;
if (is_file($path)) {
$output[] = substr($path, 2, strlen($path));
} elseif (is_dir($path)) {
$output[] = substr($path, 2, strlen($path)).'/';
$output = array_merge($output, $this->build_tree($path));
}
}
}
closedir($handle);
return $output;
}
function process_tree($tree) {
foreach( $tree as $pathfile ) {
if (substr($pathfile, -1, 1) == '/') {
fputs($this->fp, $this->build_header($pathfile));
} elseif ($pathfile != $this->tar_file) {
$filesize = filesize($pathfile);
$block_len = 512*ceil($filesize/512)-$filesize;
fputs($this->fp, $this->build_header($pathfile));
fputs($this->fp, file_get_contents($pathfile));
fputs($this->fp, pack("a".$block_len, ""));
}
}
return true;
}
function build_header($pathfile) {
if ( strlen($pathfile) > 99 ) die('Error');
$info = stat($pathfile);
if ( is_dir($pathfile) ) $info[7] = 0;
$header = pack("a100a8a8a8a12A12a8a1a100a255",
$pathfile,
sprintf("%6s ", decoct($info[2])),
sprintf("%6s ", decoct($info[4])),
sprintf("%6s ", decoct($info[5])),
sprintf("%11s ",decoct($info[7])),
sprintf("%11s", decoct($info[9])),
sprintf("%8s", " "),
(is_dir($pathfile) ? "5" : "0"),
"",
""
);
clearstatcache();
$checksum = 0;
for ($i=0; $i<512; $i++) {
$checksum += ord(substr($header,$i,1));
}
$checksum_data = pack(
"a8", sprintf("%6s ", decoct($checksum))
);
for ($i=0, $j=148; $i<7; $i++, $j++)
$header[$j] = $checksum_data[$i];
return $header;
}
}
header('Content-type: text/plain');
$start_time = array_sum(explode(chr(32), microtime()));
$tar = & new Tar_by_Vladson();
$finish_time = array_sum(explode(chr(32), microtime()));
printf("The time taken: %f seconds", ($finish_time - $start_time));
?>
engin bzzzt biz
02-Feb-2006 04:07
02-Feb-2006 04:07
To join directory and file names in a cross-platform manner you can use the following function.
function join_path()
{
$num_args = func_num_args();
$args = func_get_args();
$path = $args[0];
if( $num_args > 1 )
{
for ($i = 1; $i < $num_args; $i++)
{
$path .= DIRECTORY_SEPARATOR.$args[$i];
}
}
return $path;
}
It should do the following:
$src = join_path( '/foo', 'bar', 'john.jpg' );
echo $src; // On *nix -> /foo/bar/john.jpg
$src = join_path( 'C:\www', 'domain.com', 'foo.jpg' );
echo $src; // On win32 -> C:\\www\\domain.com\\foo.jpg
msh at onliners dot dk
30-Jan-2006 07:49
30-Jan-2006 07:49
I would like to present these two simple functions for generating a complete directory listing - as I feel the other examples are to restrictive in terms of usage.
function dirTree($dir) {
$d = dir($dir);
while (false !== ($entry = $d->read())) {
if($entry != '.' && $entry != '..' && is_dir($dir.$entry))
$arDir[$entry] = dirTree($dir.$entry.'/');
}
$d->close();
return $arDir;
}
function printTree($array, $level=0) {
foreach($array as $key => $value) {
echo "<div class='dir' style='width: ".($level*20)."px;'> </div>".$key."<br/>\n";
if(is_array($value))
printTree($value, $level+1);
}
}
Usage is as simple as this:
$dir = "<any directory you like>";
$arDirTree = dirTree($dir);
printTree($arDirTree);
It is easy to add files to the tree also - so enjoy.
Nicolas Merlet - admin(at)merletn.org
22-Jan-2006 11:34
22-Jan-2006 11:34
Here is a very similar function to *scandir*, if you are still using PHP4...
This recursive function will return an indexed array containing all directories or files or both (depending on parameters). You can specify the depth you want, as explained below.
<?php
// $path : path to browse
// $maxdepth : how deep to browse (-1=unlimited)
// $mode : "FULL"|"DIRS"|"FILES"
// $d : must not be defined
function searchdir ( $path , $maxdepth = -1 , $mode = "FULL" , $d = 0 )
{
if ( substr ( $path , strlen ( $path ) - 1 ) != '/' ) { $path .= '/' ; }
$dirlist = array () ;
if ( $mode != "FILES" ) { $dirlist[] = $path ; }
if ( $handle = opendir ( $path ) )
{
while ( false !== ( $file = readdir ( $handle ) ) )
{
if ( $file != '.' && $file != '..' )
{
$file = $path . $file ;
if ( ! is_dir ( $file ) ) { if ( $mode != "DIRS" ) { $dirlist[] = $file ; } }
elseif ( $d >=0 && ($d < $maxdepth || $maxdepth < 0) )
{
$result = searchdir ( $file . '/' , $maxdepth , $mode , $d + 1 ) ;
$dirlist = array_merge ( $dirlist , $result ) ;
}
}
}
closedir ( $handle ) ;
}
if ( $d == 0 ) { natcasesort ( $dirlist ) ; }
return ( $dirlist ) ;
}
?>
