The first method:
Copy the code The code is as follows:
/**
* Directory generation class: UtilsMakeDir
* @author yepeng
* @since 2010.3.18
*/
class UtilsMakeDir{
//This directory will not be created when the base directory is created. Establish. This should be an existing directory
private static $makeBasePath = 'video';
private static $delBasePath = 'video';
/**
* Create the directory recursively,
* Return the full path if the creation is successful,
* Return false if the creation fails
* @param String $pathString path string such as '2/3/4/5'
* @return false or string
public static function makeDir($pathString){
$pathArray = explode('/',$pathString);
if(empty($pathArray[0])){
return false;
}
$path = array_shift($pathArray) ;
self::$basePath = self::$basePath.'/'.$path;
if(is_dir(self::$basePath)){
$path = implode('/',$pathArray);
self ::makeDir($path);
}
else{
@mkdir(self::$basePath,0777);
$path = implode('/',$pathArray);
self::makeDir($path);
}
if(is_dir(self::$basePath)){
return self::$basePath;
}
else{
return false;
}
}*/
/**
* Create a directory, including the base directory. For example, if the picture is to be placed under video (video is the existing directory), the parameter you pass in should be video/2/3/4
* If the creation is successful, the full path will be returned.
* Create Return false on failure
* @param String $pathString path string such as 'video/2/3/4/5'
* @return false or string
**/
public static function makeDir ($pathString){
$pathArray = explode('/',$pathString);
$tmpPath = array_shift($pathArray);
foreach ($pathArray as $val){
$tmpPath .= "/".$val ;
if(is_dir($tmpPath)){
continue;
}
else {
@mkdir($tmpPath,0777);
}
}
if(is_dir($tmpPath)){
return $tmpPath;
}
else{
return false;
}
} /**
* Recursive deletion
* Delete directories and files
* If you pass a path like 'video/2/3/4', all directories and files under 4 will be deleted
* @param string $stringPath
*/
public static function delDir($stringPath){
if(!$handle = @opendir($stringPath)){
return false;
}
while (false !==($file = readdir($handle))){
if($file !='.' && $file != '..'){
$tmpdir = $stringPath."/" .$file;
if(is_dir($tmpdir)){
self::delDir($tmpdir);
rmdir($tmpdir);
}
if(is_file($tmpdir)){
unlink($tmpdir);
}
}
}
closedir($handle);
}}
?>
Copy the code The code is as follows:
deltree('./ copy copy copy copy copy copy copy copy aaa');
function deltree($pathdir)
{
//echo $pathdir.'
';//I use it when debugging
if(is_empty_dir($pathdir))//If it is empty
{
rmdir($pathdir);//directly Delete
}
else
{//Otherwise read this directory, except . and ..
$d=dir($pathdir);
while($a=$d->read()) //Only delete next Under $pathdir
{
if(is_file($pathdir.'/'.$a) && ($a!='.') && ($a!='..'))
{
unlink($pathdir. '/'.$a); //If it is a file, delete it directly
}elseif(is_dir($pathdir.'/'.$a) && ($a!='.') && ($a!='. .')) //If it is a directory
{
if(!is_empty_dir($pathdir.'/'.$a))//Whether it is empty
{
deltree($pathdir.'/'.$a); / /If not, call itself
}else
{
rmdir($pathdir.'/'.$a); //If it is empty, delete it directly
}
}
}
$d->close();
//echo "All files in the directory must be deleted first";//I used
rmdir($pathdir);
}
}
function is_empty_dir($pathdir)
{
//Determine whether the directory is empty. Isn’t my method very good? There are other things besides . and .. that are not empty
$d=opendir($pathdir);
$i=0;
while($a=readdir($d))
{
$i++;
}
closedir($d);
if($i>2){return false;}
else return true;
}
?>
Copy the code The code is as follows:
header("Content-Type:text/html; charset=gb2312");
if(deleteDir('./ copy copy copy copy copy copy copy copy copy copy copy aaa'))
echo " Deleted successfully";
function deleteDir($dir)
{
if (@rmdir($dir)==false && is_dir($dir)) //Cannot delete, enter to delete all files
{
if ($dp = opendir ($dir))
{
while (($file=readdir($dp)) != false)
{
if($file!='.' && $file!='..')
{ // echo $file=$dir.'/'.$file;echo '
';
$file=$dir.'/'.$file;
if (is_dir($file)) //Yes Real directory
{
deleteDir($file);
}else {
unlink($file);
}
}
}
closedir($dp);
}else
{
return false;
}
}
if (is_dir($dir) && @rmdir($dir)==false) //The directory cannot be deleted
return false;
return true;
}
?>
Copy the code The code is as follows:
function listDir($dir)
{
static $break=0; if($break++==100 ) exit;//Control the depth level
static $i=-0;
if(is_dir($dir))//Directory
{
if ($dh = opendir($dir))//Open
{
while (($file = readdir($dh)) !== false)
{
if((is_dir($dir."/".$file)) && $file!="." && $file!=". .")//Directory
{
$j=$i;while($j--) echo "-------";
echo " Directory name: ".$dir."/".$file."
The above introduces the code summary of deleting folders in Linux and recursively creating and deleting folders in PHP, including the content of deleting folders in Linux. I hope it will be helpful to friends who are interested in PHP tutorials.