php删除文件函数unlink及如何删除文件夹

2009年8月28日 由 Sken 留言 »

以实例来说明 php删除文件函数unlink 的应该:
首先创建一个文件,名为test.txt 。
*********************************
// 判断文件是否存在.
$myFile = “test.txt”;
$fh = fopen($myFile, ‘w’) or die(“can’t open file”);
fclose($fh);

// 删除文件.
$myFile = “test.txt”;
unlink($myFile);
*********************************

在PHP程序中如何删除文件夹呢
*********************************
首先先定义一个deldir函数
function deldir($dir) {
$dh=opendir($dir);
while ($file=readdir($dh)) {
if($file!=”.” && $file!=”..”) {
$fullpath=$dir.”/”.$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
if(rmdir($dir)) {
return true;
} else {
return false;
}
}

接着运用这个函数就行了,如

deldir(txt); //txt表示路径文件夹
*********************************

广告位

3 条评论

  1. 那里达人秀说道:

    不错哦 学习了 收藏下

  2. 浪美说道:

    试试····

  3. 湿式球磨机说道:

    看看能删了吗····