PHP recursive Create and delete a folder code summary _php Tips

Source: Internet
Author: User
Tags explode extend mkdir
The first method:
Copy Code code as follows:

<?php
/**
* Catalog Generation class: Utilsmakedir
* @author Yepeng
* @since 2010.3.18
*/
Class utilsmakedir{
This directory will not be established when the base directory is created for the directory. This should be a directory that already exists.
private static $makeBasePath = ' video ';
private static $delBasePath = ' video ';

/**
* Recursive directory creation,
* Establish a successful return to this full path,
* Build Failure return False
* @param string $pathString path strings 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 a base directory, such as a picture to be placed in the video (the directory where the video exists), you pass the parameter should be VIDEO/2/3/4
* Establish a successful return to this full path,
* Build Failure return False
* @param string $pathString path strings 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 a path such as ' VIDEO/2/3/4 ' is passed, 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 + recursive, test successfully under WinXP, as long as the PHP file encoding for gb2312, file name arbitrary, should be changed to encode the file name gb2312, on the line
Copy Code code as follows:

<?php
deltree ('./complex complex complex complex parts complex complex duplicate parts complex duplicate pieces aaa ');
function deltree ($pathdir)
{
echo $pathdir. ' <br/>//I use it when I'm debugging
if (Is_empty_dir ($pathdir))//If Empty
{
RmDir ($pathdir);//delete directly
}
Else
{//otherwise read this directory, except ... and. Outside
$d =dir ($pathdir);
while ($a = $d->read ())//below delete $pathdir only
{
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 it is a directory
{
if (!is_empty_dir ($pathdir. ') /'. $a))/is empty
{
deltree ($pathdir. ' /'. $a); If not, call itself
}else
{
RmDir ($pathdir. ' /'. $a); If it's empty, delete it directly.
}
}
}
$d->close ();
echo "must first delete all files under the directory";//I use it when I debug
RmDir ($pathdir);
}
}
function Is_empty_dir ($pathdir)
{
I don't think it's a good way to tell if the directory is empty. 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 under WinXP test success, as long as the PHP file code for gb2312, file name random, should be changed to encode the file name gb2312, on the line, no test
Copy Code code as follows:

<?php
Header ("content-type:text/html; charset=gb2312 ");
if (Deletedir ('./complex complex) complex complex complex complex parts duplicate complex duplicate piece complex)
echo "Delete Success";
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 ' <br/> ';
$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)//is directory is not deleted
return false;
return true;
}
?>

The third recursive method is successfully tested under WinXP, and it is very useful to list directory files.
Copy Code code as follows:

<?php
function Listdir ($dir)
{
static $break = 0; if ($break ++==100) exit;//Control depth Layer
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 "<b><font color= ' red ' > directory name:</font></b>". $dir. " /". $file." <br>$i + +;
Listdir ($dir. " /". $file);
$i--;
}
Else
{
if ($file!= "." && $file!= "...")
{
$j = $i; while ($j-) echo "-------";
$ext =trim (Extend ($file));
if ($ext = = ' jpg ')
echo $dir. '/'. $file. " <br> ";
}
}
}
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 (".");
?>

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.