PHP遞迴刪除目錄幾個代碼執行個體_PHP教程

來源:互聯網
上載者:User
下面給大家提供幾個函數參考。

執行個體一:

複製代碼 代碼如下:
function deletedir($dir){
if(!handle=@opendir($dir)){ //檢測要開啟目錄是否存在
die("沒有該目錄");
}
while(false !==($file=readdir($handle))){
if($file!=="."&&$file!==".."){ //排除目前的目錄與父級目錄
$file=$dir .DIRECTORY_SEPARATOR. $file;
if(is_dir($file)){
deletedir($file);
}else{ // www.jbxue.com
if(@unlink($file)){
echo "檔案$file刪除成功。
";
}else{
echo "檔案$file刪除失敗!
";
}
}
}
if(@rmdir($dir)){
echo "目錄$dir刪除成功了。
\n";
}else{
echo "目錄$dir刪除失敗!
\n";
}
}

//測試程式
$dir="/var/www/test";
deletedir($dir);
?>

執行個體二:Php遞迴刪除目錄方法(相容中文目錄和檔案)

複製代碼 代碼如下:
/*
本函數支援中文刪除目錄,由於我只測試了window下刪除,linux未測試,如有問題可留言或者自己稍加修改即可。
如有錯誤歡迎大家指正,共同學習
*/
header("Content-type: text/html; charset=gb2312");

function delete_dir($tmp_path){


if(!is_writable($tmp_path) && is_dir($tmp_path)){
chmod($tmp_path,0777);
}

$encode = mb_detect_encoding($tmp_path, array('UTF-8','GB2312','ASCII','GBK'));
$tmp_path = iconv($encode,'gb2312',$tmp_path);


$handle_object = scandir($tmp_path);


if(count(scandir($tmp_path))==2){

if(rmdir($tmp_path)){
echo $tmp_path.'
';
}else{
echo $tmp_path.'
';
}

return ;

}

foreach($handle_object as $val){

if($val!='..' && $val!='.' && $val!=''){

if(filetype($tmp_path.'/'.$val)=='dir'){

if(count(scandir($tmp_path.'/'.$val))==2){
if(rmdir($tmp_path.'/'.$val)){
echo $$tmp_path.'/'.$val.'
';
}else{
echo $$tmp_path.'/'.$val.'
';
}
}else{
delete_dir($tmp_path.'/'.$val);
}
}else{
if(unlink($tmp_path.'/'.$val)){
echo $$tmp_path.'/'.$val.'
';
}else{
echo $$tmp_path.'/'.$val.'
';
}
}
}else{

continue;
}

}


if(rmdir($tmp_path)){
echo $tmp_path.'
';
}else{
echo $tmp_path.'
';
}

return ;


}

delete_dir('D:/AppServ/www/testing/哈哈');

?>

執行個體三:參數$dir檔案名稱例子:admin/runtime 這樣的

複製代碼 代碼如下:
//刪除目錄及所包含檔案函數
function deldir($dir) {
//開啟檔案目錄
$dh = opendir($dir);
//迴圈讀取檔案
while ($file = readdir($dh)) {
if($file != '.' && $file != '..') {
$fullpath = $dir . '/' . $file;

//判斷是否為目錄
if(!is_dir($fullpath)) {
echo $fullpath."已被刪除
";
//如果不是,刪除該檔案
if(!unlink($fullpath)) {
}
} else {
//如果是目錄,遞迴本身刪除下級目錄
deldir($fullpath);
}
}
}
//關閉目錄
closedir($dh);
//刪除目錄
//if(rmdir($dir)) {
// return true;
// } else {
// return false;
// }
}

執行個體四:

複製代碼 代碼如下:
function deldir($dirname){
if(file_exists($dirname)){//首先判斷目錄是否有效
$dir = opendir($dirname);//用opendir開啟目錄
while($filename = readdir($dir)){//使用readdir迴圈讀取目錄裡的內容
if($filename != "." && $filename != ".."){//排除"."和".."這兩個特殊的目錄
$file = $dirname."/".$filename;
if(is_dir($file)){//判斷是否是目錄,如果是則調用自身
deldir($file); //使用遞迴刪除子目錄
}else{
@unlink($file);//刪除檔案
}
}
}
closedir($dir);//關閉檔案操作控制代碼
rmdir($dirname);//刪除目錄
}
}
?>

執行個體五:

複製代碼 代碼如下:
/**
* 刪除非空目錄
* @method rrmdir
*/
function rrmdir ($dir) {

if (is_dir($dir)) {

$fs = array_slice(scandir($dir), 2);
foreach ($fs as $f) {
$path = $dir . '/' . $f;
is_dir($path) ? rrmdir($path) : unlink($path);
}
reset($fs);
return rmdir($dir);
}

}

執行個體六:

複製代碼 代碼如下:
function del_dir( $dir )
{
if ( $handle = opendir( $dir ) )
{
while ( false !== ( $item = readdir( $handle ) ) )
{
if ( $item != "." && $item != ".." )
{
if ( is_dir( "$dir/$item" ) )
{
del_dir( "$dir/$item" );
}
else
{
unlink( "$dir/$item" ) ;
}
}
}
closedir( $handle );
rmdir( $dir ) ;

}
}
?>

http://www.bkjia.com/PHPjc/756995.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/756995.htmlTechArticle下面給大家提供幾個函數參考。 執行個體一: 複製代碼 代碼如下: ?php function deletedir($dir){ if(!handle=@opendir($dir)){ //檢測要開啟目錄是否存在 di...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.