转载的http://www.phpx.com/happy/viewthread.php?tid=84709
- <?php
- /* *
- +---------------------------------------+
- | The function Of Delete Directory |
- +---------------------------------------+
- | Author: Wang Bo |
- +---------------------------------------+
- | Email: [email]Misterwangbo@hotmail.com[/email] |
- +---------------------------------------+
- | The Function May Delete File From |
- | Your Appointed Directory |
- | 因为前面的while语句已经判断了"."和".."两 |
- | 种情况. 所以因为指针的缘故可以直接用 |
- | if (readdir($handle) == false)判断目录 |
- | 是否为空 |
- +---------------------------------------+
- | 调用方法:my_del_dir("目录名"); |
- +---------------------------------------+
- | Last Update: 2004-05-09 |
- +---------------------------------------+
- * */
- function my_del_dir($directory)
- {
- if (is_dir($directory) == false)
- {
- exit("The Directory Is Not Exist!");
- }
- $handle = opendir($directory);
- while (($file = readdir($handle)) !== false)
- {
- if ($file != "." && $file != "..")
- {
- is_dir("$directory/$file")?
- my_del_dir("$directory/$file"):
- unlink("$directory/$file");
- }
- }
- if (readdir($handle) == false)
- {
- closedir($handle);
- rmdir($directory);
- }
- }
- ?>
- <?php
- /* *
- +---------------------------------------+
- | The Function Of Copy Directory |
- +---------------------------------------+
- | Author: Wang Bo |
- +---------------------------------------+
- | Email: [email]misterwangbo@hotmail.com[/email] |
- +---------------------------------------+
- | The Function May Copy File From Your |
- | Source Directory to Your Destinction |
- | Directory. |
- +---------------------------------------+
- | Last Update: 2004-05-09 |
- +---------------------------------------+
- * */
- function my_copy_dir($source, $destination)
- {
- if (is_dir($source) == false)
- {
- exit("The Source Directory Is Not Exist!");
- }
- if (is_dir($source) == false)
- {
- mkdir($destination, 0700);
- }
- $handle=opendir($source);
- while (false !== ($file = readdir($handle)))
- {
- if ($file != "." && $file != "..")
- {
- is_dir("$source/$file")?
- my_copy_dir("$source/$file", "$destination/$file"):
- copy("$source/$file", "$destination/$file");
- }
- }
- closedir($handle);
- }
- ?>
- <?php
- /* *
- +---------------------------------------+
- | 判断目录是否为空 |
- +---------------------------------------+
- | Author: Wang Bo |
- +---------------------------------------+
- | Email: [email]Misterwangbo@hotmail.com[/email] |
- +---------------------------------------+
- | 判断目录是否为空 |
- +---------------------------------------+
- | Last Update: 2004-05-09 |
- +---------------------------------------+
- * */
- function my_judge_empty_dir($directory)
- {
- $handle = opendir($directory);
- while (($file = readdir($handle)) !== false)
- {
- if ($file != "." && $file != "..")
- {
- closedir($handle);
- return false;
- }
- }
- closedir($handle);
- return true;
- }
- ?>
- <?php
- function copyDir($source, $destination)
- {
- $result = true;
- if(! is_dir($source))
- {
- trigger_error("源目录名称错误", E_USER_ERROR);
- }
- if(! is_dir($destination))
- {
- if(! mkdir($destination, 0700))
- {
- trigger_error("无法创建目标目录", E_USER_ERROR);
- }
- }
- $handle = opendir($source);
- while(($file = readdir($handle)) !== false)
- {
- if($file != '.' && $file != '..')
- {
- $src = $source . DIRECTORY_SEPARATOR . $file;
- $dtn = $destination . DIRECTORY_SEPARATOR . $file;
- if(is_dir($src))
- {
- copyDir($src, $dtn);
- }
- else
- {
- if(! copy($src, $dtn))
- {
- $result = false;
- break;
- }
- }
- }
- }
- closedir($handle);
- return $result;
- }
- ?>
