Home  >  Article  >  Backend Development  >  Linux delete folder PHP code summary to recursively create and delete folders

Linux delete folder PHP code summary to recursively create and delete folders

WBOY
WBOYOriginal
2016-07-29 08:48:241722browse

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);
}}
?>


loop + recursion, tested successfully under winxp, as long as the php file is encoded as gb2312 and the file name is arbitrary, the file name should be changed to The encoding is gb2312, just

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;
}
?>


The second recursive method was tested under winxp Success, as long as the php file is encoded as gb2312 and the file name is arbitrary, the file name should be changed to gb2312 and it will be fine. No test

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;
}
?>


The third recursive method was successfully tested under winxp , it is very useful to list directory files

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."


";
$i++;
listDir($dir."/". $file);
$i--;
}
else
{
if($file!="." && $file!="..")
{
$j=$i;while($j- -) echo "-------";
$ext=trim(extend($file));
//if($ext=='jpg')
echo $dir.'/'.$file ."
";
}
}
}
closedir($dh);
}
}
}
function extend($file_name)
{
$retval="";
$pt=strrpos($ file_name, ".");
if ($pt) $retval=substr($file_name, $pt+1, strlen($file_name) - $pt);
return ($retval);
}
//Start running
listDir(".");
?>

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.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn