您的位置:首页 > 编程语言 > PHP开发

php学习笔记3--文件系统的操作(创建、打开及批量删除)

2014-08-12 21:50 751 查看
文件操作:

resource fopen(string name,string mode); 打开文件或远程url

bool fclose(resource handle);关闭打开的文件资源

bool is_readable(string name); 判断文件是否可读

string file_get_contents(string name);读取文件内容

array file(string name);读取文件内容每一行为数组一个元素

bool is_writable(string name);判断文件是否可写

bool fwrite(resource handle,string string);写入内容

int file_put_contents(string name,string date,FILE_APPEND);写入内容

bool file_exists(string name);判断文件或目录存在不存在

目录管理:

bool is_dir(string dirname);判断是不是目录

bool mkdir(string pathname);建立目录

resource opendir(string path);打开目录,目录打开,目录流指定头

string readdir(resource dir_handel);目录读一次,流自动下移,到结尾时,readdir()返回false;

void closedir(resource dir_handel);关闭打开的目录

bool rmdir(string dirname);//只能删除空目录或空文件夹,非空无法删除

array scandir(string directory);返回目录中的目录名和文件名组成的数组

bool copy ( string $source , string $dest [, resource $context ] )

bool rename ( string $oldname , string $newname [, resource $context ] )

bool file_exists ( string $filename )

bool unlink ( string $filename [, resource $context ] )

bool is_file ( string $filename )判断给定文件名是否为一个正常的文件

递归批量删除非空目录:(函数如下)

function delTree($dir) {

$files = array_diff(scandir($dir), array('.','..'));

foreach ($files as $file) {

(is_dir("$dir/$file") && !is_link($dir)) ? delTree("$dir/$file") : unlink("$dir/$file");

}

return rmdir($dir);

}

统计某一种类型(如php)的文件个数:

p = './'; //指当前目录

$count = 0;

function ss($dir){

global $count;

$ds = scandir($dir);

foreach($ds as $v){

if($v=='.' || $v=='..'){ continue;}

$path = $dir.$v;

if(is_file($path)){

if(strrchr($v,'.php')=='.php'){

$count++;

}

}else{

ss($path.'/');

}

}

}

ss($p);

echo $count;

批量删除文件(包括本文件夹),如下:

function del($dir){

$ds = scandir($dir);

foreach($ds as $v){

if($v=='.' || $v=='..'){ continue;}

$path = $dir.$v;

if(is_file($path)){

unlink($path);

}else{

del($path.'/');

}

}

return rmdir($dir);

}

del('./a/');

del('c:/');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: