readdir函数 php,PHP: readdir - Manual

A very flexible function to recursively list all files in a directory with the option to perform a custom set of actions on those files and/or include extra information about them in the returned data.

----------

SYNTAX:

$array = process_dir ( $dir , $recursive = FALSE )

$dir  (STRING)   =  Directory to process

$recursive  (BOOLEAN)  =  [Optional] Recursive if set to TRUE

RETURN VALUES:

The function returns an indexed array, one entry for every file. Each entry is an associative array, containing the basic information 'filename' (name of file) and 'dirpath' (directory component of path to file), and any additional keys you configure. Returns FALSE on failure.

----------

To allow you to configure another key, the entry for each file is stored in an array, "$entry" for each iteration. You can easily return any additional data for a given file using $entry['keyname'] = ... (Note that this data can be any variable type - string, bool, float, resource etc)

There is a string variable "$path" available, which contains the full path of the current file, relative to the initial "$dir" supplied at function call. This data is also available in it's constituent parts, "$dir" and "$file". Actions for each file can be constructed on the basis of these variables. The variables "$list", "$handle" and "$recursive" should not be used within your code.

----------

Simply insert you code into the sections indicated by the comments below and your away!

The following example returns filename, filepath, and file modified time (in a human-readable string) for all items, filesize for all files but not directories, and a resource stream for all files with 'log' in the filename (but not *.log files).

if (is_dir($dir)) {

for ($list= array(),$handle=opendir($dir); (FALSE!== ($file=readdir($handle)));) {

if (($file!='.'&&$file!='..') && (file_exists($path=$dir.'/'.$file))) {

if (is_dir($path) && ($recursive)) {$list=array_merge($list,process_dir($path,TRUE));

} else {$entry= array('filename'=>$file,'dirpath'=>$dir);//---------------------------------------------------------//

//                     - SECTION 1 -                       //

//          Actions to be performed on ALL ITEMS           //

//-----------------    Begin Editable    ------------------//$entry['modtime'] =filemtime($path);//-----------------     End Editable     ------------------//do if (!is_dir($path)) {//---------------------------------------------------------//

//                     - SECTION 2 -                       //

//         Actions to be performed on FILES ONLY           //

//-----------------    Begin Editable    ------------------//$entry['size'] =filesize($path);

if (strstr(pathinfo($path,PATHINFO_BASENAME),'log')) {

if (!$entry['handle'] =fopen($path,r))$entry['handle'] ="FAIL";

}//-----------------     End Editable     ------------------//break;

} else {//---------------------------------------------------------//

//                     - SECTION 3 -                       //

//       Actions to be performed on DIRECTORIES ONLY       //

//-----------------    Begin Editable    ------------------//

//-----------------     End Editable     ------------------//break;

} while (FALSE);$list[] =$entry;

}

}

}closedir($handle);

return$list;

} else returnFALSE;

}$result=process_dir('C:/webserver/Apache2/httpdocs/processdir',TRUE);// Output each opened file and then closeforeach ($resultas$file) {

if (is_resource($file['handle'])) {

echo"\n\nFILE (".$file['dirpath'].'/'.$file['filename'] ."):\n\n".fread($file['handle'],filesize($file['dirpath'].'/'.$file['filename']));fclose($file['handle']);

}

}?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值