-
-
/** - * Directory generation class: UtilsMakeDir
- * @author yepeng
- * @since 2010.3.18
- */
- class UtilsMakeDir{
- //base directory This directory will not be created when creating the directory. 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 p>
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 Failure returns 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 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 code
Loop and recursion, under winxp The test is successful. As long as the PHP file is encoded as gb2312 and the file name is arbitrary, the file name should be changed to gb2312.
-
- deltree('./copy copy copy copy copy copy copy copy copy aaa');
- function deltree($pathdir)
- {
- //echo $pathdir.'
if(is_empty_dir($pathdir))//If it is empty - {
- rmdir($pathdir);//Delete it directly
- }
- else
- {//Otherwise read this directory , except . and ..
- $d=dir($pathdir);
- while($a=$d->read()) //Only delete $pathdir next
- {
- if(is_file($pathdir. '/'.$a) && ($a!='.') && ($a!='..'))
- {
- unlink($pathdir.'/'.$a); //If it is a file Just delete it directly
- }elseif(is_dir($pathdir.'/'.$a) && ($a!='.') && ($a!='..')) //If it is a directory
- {
- if (!is_empty_dir($pathdir.'/'.$a))//Is it 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" ;//What I use when debugging is
- rmdir($pathdir);
- }
- }
- function is_empty_dir($pathdir)
- {
- //My method of judging whether the directory is empty is not very good, right? Except for. and.. There is something else that 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 2
The test was successful 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 gb2312 and it will be fine. No test.
-
- header("Content-Type: text/html; charset=gb2312");
- if(deleteDir('./ copy copy copy copy copy copy copy copy copy copy copy copy aaa') )
- echo "Delete successfully";
- function deleteDir($dir)
- {
- if (@rmdir($dir)==false && is_dir($dir)) //Cannot 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) //The directory cannot be deleted
- return false;
- return true;
- }
- ?>
Copy code
method 3. The test was successful under winxp. It is very useful to list directory files.
-
- function listDir($dir)
- {
- static $break=0; if($break++==100) exit;//Control the number of depth levels
- 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
|