问题描述
- 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; } }
只要目录名字是中文或者目录里包含中文文件,这个目录就删除不了,甚是头疼,求解!
看看这个递归删除目录的函数,哪里需要修改?
解决方案
解决方案二:
遍历目录
$dir = 'C:UsersDDesktopbooks'; $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"; }}