search
Homephp教程php手册php删除不是空目录实现代码

php删除不是空目录实现代码

Jun 13, 2016 am 10:00 AM
phprmdirnocodefunctiondeleteaccomplishofTable of contentsBring your own

php中自带的函数rmdir只能删除空目录,如果你要删除带有文件或有目录的目录,需要递归与unlink一起使用了,下面我们看看删除不是空目录的自定义函数。

php教程删除不是空目录实现代码
本教程先是简单简介了rmdir来删除空目录,然后再引伸到删除不是空目录的自定义函数的写法与实现代码。
*/
//rmdir(dir,context) rmdir() 函数删除空的目录。

$path ='';
if( is_dir( $path ) )
{
 if( rmdir( $path ) )
 {
  echo '删除目录成功';
 }
}
else
{
 echo '不是目录';
}

/*
总结
    php中自带的函数rmdir只能删除空目录,如果你要删除带有文件或有目录的目录,需要递归与unlink一起使用了,下面我们看看删除不是空目录的自定义函数。
*/
/**
 * 删除文件或文件夹(递归)
 * @param array $filelist
 * @param string $option
 * @param string $fileext 要删除的文件扩展名 格式:'html'
 * @return void
 */

 function rm($filelist, $option='r', $fileext = null, $if_rmdir = false) {
  if (!is_array($filelist)) {
   $filelist = explode('|', $filelist);
  }
  foreach ($filelist as $filename) {
   if (is_file($filename)) {
    if (empty($fileext)) {
     unlink($filename);
    } else {
     if (substr(strrchr($filename, '.'), 1 ) == $fileext){
      unlink($filename);
     }
    }
   } elseif (is_dir($filename)) {
    if (strpos($option, 'r')!==false) {
     $file_list_ = ls($filename);
     foreach ($file_list_ as $fi => $file) {
      $file_list_[$fi] = $filename . $file;
     }
     rm($file_list_, $option, $fileext);
    }
    if ($if_rmdir) {
     rmdir($filename);
    }
   }
  }
 }

//调用方法

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft