suyan
9/5/2012 - 6:56 AM

PHP: Delete all file of dir

PHP: Delete all file of dir

function delete_files($dir){
	if($handle = opendir($dir)){
		while (false !== ($file = readdir($handle))){
			if($file=='.' OR $file=='..')
				continue;
			$file=$dir.$file;
			if(is_file($file) && filemtime($file)<time()-24*60*60){
				unlink($file);
			}else if(is_dir($file)){
				delete_files($file.DIRECTORY_SEPARATOR);
				rmdir($file);
			}
		}
		closedir($handle);
	}	
}
<?php
function delete_file($dir,$self=false){
	if(false === is_dir($dir)){
		if($self==false)
			return false;
		unlink($dir);
		return true;
	}
	if($handle = opendir($dir)){
		while (false !== ($file = readdir($handle))){
			if($file=='.' OR $file=='..')
				continue;
			$file=$dir.$file;
			if(is_file($file)){
				unlink($file);
			}else if(is_dir($file)){
				delete_file($file.DIRECTORY_SEPARATOR,true);
			}
		}
		closedir($handle);
		if($self==true)
			rmdir($dir);
	}
}