1、创建目录(mkdir)
bool mkdir (string$pathname [,int$mode [,bool$recursive [,resource$context ]]] )
<?php
mkdir("/path/to/my/dir", 0777); //成功返回true,失败返回false;
?>
2、判断文件是否存在(file_exist)
bool file_exists (string$filename )
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
3、目录是否存在(is_dir)
bool is_dir (string$filename )
<?
var_dump(is_dir('a_file.txt'));// 输出false
var_dump(is_dir('bogus_dir/abc')); //输出false
var_dump(is_dir('..')); //输出true
?>
4、判断给定文件名是否为一个正常的文件 (is_file)
bool is_file ( string$filename )
<?php
var_dump(is_file('a_file.txt'));//true
var_dump(is_file('/usr/bin/'));//false
?>
bool is_link ( string$filename )
<?php
var_dump(is_link("a.lnk")); //输出true
?>
bool rmdir ( string$dirname )
<?php
var_dump(rmdir("/usr/local/a")); //只能删除空目录,当a为空目录删除成功,a为非空目录删除失败
?>
bool unlink ( string$filename )
<?
while(is_file($data_file_to_delete) == TRUE)
{
chmod($data_file_to_delete, 0666);
unlink($data_file_to_delete);
}
?>
8、读取目录文件 (opendir,readir,closedir)
resource opendir ( string$path [,resource$context ] )
string readdir ( resource$dir_handle )
voidclosedir (resource $dir_handle )
<?php
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
?>
9、扩展函数,方法
删除目录及目录下所有文件夹
public function rmdir_tree($dirname){
if(is_file($dirname)) unlink($dirname);
$hd = opendir($dirname);
while(false !== ($file = readdir($hd))){
if($file == "." || $file == ".."){
continue;
}
$path = $dirname."/".$file;
if(is_dir($path)){
rmdir_tree($path);
}elseif (is_file($path)){
unlink($path);
}
}
closedir($hd);
rmdir($dirname);
}