目录操作
opendir();
readdir()
closedir();
检索目录 * ? {}
glob("webchat/{c,d,e,f}*",GLOB_BRACE);
$file=glob("webchat/{c,d,e,f}*",GLOB_BRACE);
foreach($file as $key){
echo $key."<br/>";
}
dir — 返回一个 Directory 类实例
$d=dir("webchat");
echo "路径是".$d->path."<br/>";
echo "引用句柄".$d->handle."<br/>";
$d->read();
while(($file=$d->read())!==false){
if($file!="."&& $file!=".."){
echo $file."<br>";
}
}
$d->close();
读取目录结构
$dir=opendir("D:/apache2.4/htdocs/phpMyAdmin")or die("打开目录不成功<br/>");
echo "phpmyadmin目录下所以内容信息是:<br>";
readdir($dir)."<br/>";
readdir($dir)."<br/>";
//echo DIRECTORY_SEPARATOR."<br/>";
while(($file=readdir($dir))!==false){
if($file!="."&&$file !=".."){
if(is_dir("D:/apache2.4/htdocs/phpmyadmin"."/".$file)){
echo "目录:".$file."<br/>";
}else{
echo "file".$file."文件大小".filesize("D:/apache2.4/htdocs/phpmyadmin"."/".$file)."<br/>";
}
}
}
rewinddir($dir);
closedir($dir);
建立目录 mkdir($pathname,)
批量删除文件或者目录:
<?php
header("Content-type:text/html;charset:utf-8");
$file="webchat";
function deleteDir($PathName){
$handle=opendir($PathName);
readdir($handle);
readdir($handle);
while(($file=readdir($handle))!==false){
$file=$PathName.DIRECTORY_SEPARATOR.$file;
if(is_dir($file)){
deleteDir($file);
}else{
if(unlink($file)){
echo "文件.$file 删除成功<br/>";
}else{
echo "文件.$file 删除失败<br/>";
}
}
} closedir($handle);
if(rmdir($PathName)){
echo "目录".$PathName."删除成功";
}else{
echo "目录$pathName删除失败";
}
}
deleteDir($file);
?>
复制目录;
<?php
header("Content-type:text/html;charset:utf-8");
$filecounter=0;
$dircounter=0;
function copydir($dirFrom,$dirTo)
{
global $filecounter;
global $dircounter;
if(is_file($dirTo))
{
die("无法创建目录 $dirTo");
}
if(!file_exists($dirTo)){
mkdir($dirTo);
$dircounter++;
}
$handle=opendir($dirFrom);
readdir($handle);
readdir($handle);
while(($file=readdir($handle))!==false)
{
$fileFrom=$dirFrom.DIRECTORY_SEPARATOR.$file;
$fileTo=$dirTo.DIRECTORY_SEPARATOR.$file;
if(is_dir($fileFrom))
{
copydir($fileFrom,$fileTo);
}else{
copy($fileFrom,$fileTo);
$filecounter++;
}
}
closedir($handle);
}
copydir("webchat","web3");
echo "共拷贝文件".$filecounter."个<br/>";
echo "共拷贝目录".$dircounter."个";
?>