/**
- * 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);
- }}
- ?>
Copy CodeLoop and recursion, 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 it.
- 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;
- }
- ?>
Copy Code Method Two in 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 line, not measured.
-
- !--? php header ("content-type:text/html; charset=gb2312 ");
- if (Deletedir ('./copy-copy-copy-copy-copy-duplicate-copy-copy-duplicate-copy-re---------")
- echo "Delete succeeded";
- function Deletedir ($dir)
- {
- if (@rmdir ($dir) ==false && is_dir ($dir))//delete, 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 cannot be deleted
- return false;
- return true;
- }
- ?
Copy CodeMethod Three, under WinXP test success, is to list the directory file is very useful.
- function Listdir ($dir)
- {
- static $break = 0; if ($break ++==100) exit;//control depth of 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 " 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 (".");
- ?>
Copy Code |