2 ssxszt ssxszt 于 2013.11.19 10:34 提问

php如何递归删除中文目录?
function delete_folder($tmp_path){
    if(!is_writable($tmp_path) && is_dir($tmp_path)){
        chmod($tmp_path,0777);
    }
    $handle = opendir(iconv('utf-8','gb2312',$tmp_path)); 
    while(($tmp=readdir($handle)) !== false){
        if($tmp!='..' && $tmp!='.' && $tmp!=''){ 
            if(is_writable($tmp_path.'/'.$tmp) && is_file($tmp_path.'/'.$tmp)){ 
                unlink($tmp_path.'/'.$tmp); 
            }elseif(!is_writable($tmp_path.'/'.$tmp) && is_file($tmp_path.'/'.$tmp)){
                chmod($tmp_path.'/'.$tmp,0666); 
                unlink($tmp_path.'/'.$tmp); 
            } 

            if(is_writable($tmp_path.'/'.$tmp) && is_dir($tmp_path.'/'.$tmp)){
                delete_folder($tmp_path.'/'.$tmp);  
            }elseif(!is_writable($tmp_path.'/'.$tmp) && is_dir($tmp_path.'/'.$tmp)){ 
                chmod($tmp_path.'/'.$tmp,0777); 
                delete_folder($tmp_path.'/'.$tmp);
            } 
        }
    }
    closedir($handle);
    rmdir(iconv('utf-8','gb2312',$tmp_path));
    if(!is_dir($tmp_path)){
        return true;
    }else{
        return false;
    }
}

只要目录名字是中文或者目录里包含中文文件,这个目录就删除不了,甚是头疼,求解!
看看这个递归删除目录的函数,哪里需要修改?

2个回答

franzhong
franzhong   2013.11.21 12:35
hjq_tlq
hjq_tlq   2014.01.20 16:10

遍历目录

$dir = 'C:\Users\D\Desktop\books'; $it = new
RecursiveDirectoryIterator($dir); $iterator = new
RecursiveIteratorIterator($it,
RecursiveIteratorIterator::CHILD_FIRST); foreach($iterator as $path) {
echo $path->__toString() , '
'; }

//循环删除目录和文件函数

function delDirAndFile( $dirName ) { if ( $handle = opendir(
"$dirName" ) ) { while ( false !== ( $item = readdir( $handle ) )
) { if ( $item != "." && $item != ".." ) { if ( is_dir(
"$dirName/$item" ) ) { delDirAndFile( "$dirName/$item" ); }
else { if( unlink( "$dirName/$item" ) )echo "成功删除文件:
$dirName/$item
\n"; } } } closedir( $handle
); if( rmdir( $dirName ) )echo "成功删除目录: $dirName
\n"; }

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!