Linux Delete folder php recursive code summary for creating and deleting folders

Source: Internet
Author: User
The first method:

Copy the Code code as follows:


/**
* Directory Generation class: Utilsmakedir
* @author Yepeng
* @since 2010.3.18
*/
Class utilsmakedir{
The directory is not established when the base directory is created. This should be a directory that already exists.
private static $makeBasePath = ' video ';
private static $delBasePath = ' video ';
/**
* Recursively create a directory,
* Establish a successful return to this full path,
* Build failed return False
* @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;
}
} */
/**
* Set up the directory, including the base directory, the chip to be placed in the video (video for the existence of the directory), your incoming parameters should be VIDEO/2/3/4
* Establish a successful return to this full path,
* Build failed return False
* @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 such as ' 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::d eldir ($tmpdir);
RmDir ($tmpdir);
}
if (Is_file ($tmpdir)) {
Unlink ($tmpdir);
}
}
}
Closedir ($handle);
}}
?>


Loop + recursion, in the WinXP test success, as long as the PHP file encoded as gb2312, the file name is arbitrary, should be changed to encode the filename gb2312, on the line

Copy the Code code as follows:


deltree ('./copy copy of duplicate copy of duplicate copy of duplicate copy of the re-piece of AAA ');
function deltree ($pathdir)
{
echo $pathdir. '
';//I use it for debugging
if (Is_empty_dir ($pathdir))//If it is empty
{
RmDir ($pathdir);//delete directly
}
Else
{//otherwise read this directory except for. and. Outside
$d =dir ($pathdir);
while ($a = $d->read ())//under Delete only $pathdir
{
if (Is_file ($pathdir. ' /'. $a) && ($a! = '. ') && ($a! = ' ... '))
{
Unlink ($pathdir. ' /'. $a); If it's a file, delete it directly.
}elseif (Is_dir ($pathdir. ' /'. $a) && ($a! = '. ') && ($a! = ' ... ')) If the directory
{
if (!is_empty_dir ($pathdir. ' /'. $a))//Is empty
{
deltree ($pathdir. ' /'. $a); If not, call itself
}else
{
RmDir ($pathdir. ' /'. $a); If it is empty, delete it directly.
}
}
}
$d->close ();
echo "must first delete all files in the directory";//I use it for debugging
RmDir ($pathdir);
}
}
function Is_empty_dir ($pathdir)
{
I'm not good at judging if the directory is empty, right? besides. and. Something else is 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 in WinXP test success, as long as the PHP file encoded as gb2312, file name arbitrary, should be changed to encode the file name gb2312, on line, not measured

Copy the Code code as follows:


Header ("content-type:text/html; charset=gb2312 ");
if (Deletedir ('./copy copy of duplicate copy of duplicate copy of duplicate copy of the copy)
echo "Delete succeeded";
function Deletedir ($dir)
{
if (@rmdir ($dir) ==false && is_dir ($dir))//deleted, go 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))//is the real directory
{
Deletedir ($file);
}else {
Unlink ($file);
}
}
}
Closedir ($DP);
}else
{
return false;
}
}
if (Is_dir ($dir) && @rmdir ($dir) ==false)//directory is not deleted
return false;
return true;
}
?>


The third recursive method was tested successfully under WinXP, which is to list the directory files very useful

Copy the Code code as follows:


!--? php function listdir ($dir)
{
static $break =0; if ($break ++==100) exit;//control in-depth layer
static $i = -0;
if (Is_dir ($dir))//directory
{
if ($dh = Opendir ($dir))//open
{
while (($file = Readdir ($DH))!== false)
{ Br>if ((Is_dir ($dir. " /". $file)) && $file! =". && $file! = "...") The 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 describes the Linux Delete folder php recursive creation and deletion of the folder code summary, including the contents of the Linux Delete folder, I hope to be interested in PHP tutorial friends helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.