用PHP获取指定目录的文件列表,常见的有2种方法:
- 主要用到PHP的dir()/is_dir()函数;
- 用到PHP的opendir()/readdir()/closedir()/realpath()/is_dir()等函数。
方法一:
<?php
print_r(getDirFiles("./", true, true));
print_r(getDirFiles("./", true));
function getDirFiles($path,$subDir=false/*是否遍历子目录*/,$addDir=false/*是否输出文件夹*/) {
$mydir=dir($path);
$all=array();
while( ($file=$mydir->read())!==false){
if($file=="." || $file==".."){
continue;
}
if ( is_dir( $path.$file ."/") ) {
if($addDir) {
$all[]=$path.$file ."/";
}
if($subDir) {
$temp=getDirFiles( $path.$file ."/" ,$sub );
$all=array_merge($all,$temp);
}
} else {
$all[]= $path.$file ;
}
}
return $all;
}
?>
方法二:
<?php
/*
*递归获取指定路径下的所有文件或匹配指定正则的文件(不包括“.”和“..”),结果以数组形式返回
* @param string $dir
* @param string $pattern
* @return array
*/
function file_list($dir,$pattern="")
{
$arr=array();
$dir_handle=opendir($dir);
if($dir_handle)
{
// 这里必须严格比较,因为返回的文件名可能是“0”
while(($file=readdir($dir_handle))!==false)
{
if($file==='.' || $file==='..')
{
continue;
}
$tmp=realpath($dir.'/'.$file);
if(is_dir($tmp))
{
$retArr=file_list($tmp,$pattern);
if(!empty($retArr))
{
$arr[]=$retArr;
}
}
else
{
if($pattern==="" || preg_match($pattern,$tmp))
{
$arr[]=$tmp;
}
}
}
closedir($dir_handle);
}
return $arr;
}
// 列出当前目录下所有以".php"扩展名(不区分大小写)结尾的文件
echo '<pre>';
print_r(file_list(dirname(__FILE__),'/.php$/i'));
echo '</pre>';
?>
附录一:用PHP获取当前目录的方法
<?php
// 获取当前目录
echo realpath(".")."n";
echo getcwd() . "n";
echo dirname(__file__) . "n"; //PHP 常量
// 获取上级目录
echo realpath(".."). "n";
echo dirname(dirname(__FILE__)) . "n"; //取得当前文件的上一层目录
// 获取网站根目录
echo $_SERVER['DOCUMENT_ROOT'] . "n";
// 获取目录信息
$path_parts = pathinfo(__FILE__);
echo $path_parts["dirname"] . "n"; //目录名
echo $path_parts["basename"] . "n";//文件名
echo $path_parts["extension"] . "n";//扩展名
echo dirname(__FILE__) . "n";//目录名--PHP常量
?>
附录二:批量重命名文件(去掉.bak的后缀)
<?php
//php批量重命名文件(去掉.bak)
function foreachDir($path){
if(!is_dir($path)){
echo "{$path}不是一个有效的目录!";
exit();
}
$handle=opendir($path);
if($handle){
while (($file = readdir($handle))!==false) {
if($file!='.' && $file!='..'){
if(is_dir($path.$file)){
echo $path.$file."<br/>n";
foreachDir($path.$file);
} else{
$bak = pathinfo($file);
if($bak[extension]==bak){
$ext = strripos($file,'.');
$newname = substr($file,0,$ext);
echo $path.'/'.$file.'---'.$path.'/'.$newname."<br/>n";
rename($path.'/'.$file,$path.'/'.$newname);
}
}
}
}
return false;
}
}
foreachDir('./');
?>
差不多就这些方法了~
补充:
function deleteDirAndFile( $dirName )//递归删除指定目录
{
if ( $handle = opendir( "$dirName" ) ) {
while ( ($item = readdir( $handle ))!==false ) {
if ( $item != "." && $item != ".." ) {
if ( is_dir( "$dirName/$item" ) ) {
deleteDirAndFile( "$dirName/$item" );
} else {
@unlink( "$dirName/$item" );//删除文件unlink()
}
}
}
closedir( $handle );
@rmdir( $dirName ); //rmdir()删除指定的目录,该目录必须是空的,而且要有相应的权限
}
}
再补充一个:
<?php
function getDirFiles($path,$subDir=false/*是否遍历子目录*/,$printDir=false/*是否输出文件夹*/) {
$mydir=dir($path);
$all=array();
while( ($file=$mydir->read())!==false){
if($file=="." || $file==".."){
continue;
}
if ( is_dir( $path.DIRECTORY_SEPARATOR.$file) ) {
if($printDir) {
$all[]=$path.DIRECTORY_SEPARATOR.$file;
}
if($subDir) {
$temp=getDirFiles( $path.DIRECTORY_SEPARATOR.$file, $subDir );
$all=array_merge($all,$temp);
}
} else {
$all[]= $path.DIRECTORY_SEPARATOR.$file ;
}
}
return $all;
}
print_r(getDirFiles(getcwd()), true);
$filelist = array();
function get_file_list($dbsource) {
global $filelist;
$current_file_list = glob($dbsource);
foreach ($current_file_list as $each) {
if (strpos($each, 'search.php') === true)
continue;
if (is_file($each))
$filelist[] = $each;
if (is_dir($each))
get_file_list($each . '\*');
}
}
get_file_list(getcwd() . '\*');
print_r($filelist);
function getDirFiles_r($path){
$mydir = opendir($path);
$all = array();
while ( false !== ($file=readdir($mydir)) ){
if($file == "." || $file == ".."){
continue;
}
$tmp = $path.DIRECTORY_SEPARATOR.$file;
if( is_dir($tmp) ){
$temp = getDirFiles_r($tmp);
$all = array_merge($all, $temp);
} else {
$all[] = $tmp;
}
}
return $all;
}
print_r(getDirFiles_r(getcwd()));
?>