Home Backend Development PHP Tutorial PHP delete entire directory matching criteria

PHP delete entire directory matching criteria

Aug 08, 2016 am 09:25 AM
false path

<span><?<span>php

    </span><span>/*</span><span>*
    *    @name       delFile函数与delDir函数一起使用, 删除符合条件的整个目录
    *    @param      string  $path   指定操作路径
    *    @return     null
    *    @example    delDir('D:\web\Apache\htdocs\KeyShareMall\Pc\ThinkPHP');
    </span><span>*/<br />  <br /></span>   // 删除目录
    <span>function</span> delFile(<span>$path</span><span>)
    {
        </span><span>if</span> (<span>empty</span>(<span>$path</span><span>)) {

            </span><span>echo</span> '请指定要操作的文件路径'<span>;
            </span><span>return</span> <span>false</span><span>;

        }

        </span><span>if</span> ( <span>$handle</span>  =  <span>opendir</span> ( <span>$path</span><span> )) {

            </span><span>while</span> ( <span>false</span>  !== ( <span>$fileName</span>  =  <span>readdir</span> ( <span>$handle</span><span> ))) {

                </span><span>if</span> ( <span>$fileName</span>  !=  "."  &&  <span>$fileName</span>  !=  ".."<span> ) {
                    
                    </span><span>if</span> (<span>is_file</span>(<span>$path</span> . '/' . <span>$fileName</span><span>)) {

                        </span><span>unlink</span>(<span>$path</span> . '/' . <span>$fileName</span><span>);                    
                            
                    }

                    </span><span>if</span> (<span>is_dir</span>(<span>$path</span> . '/' . <span>$fileName</span><span>)) {

                        delFile(</span><span>$path</span> . '/' . <span>$fileName</span><span>);
                        
                    }

                }

            }

            </span><span>rmdir</span>(<span>$path</span><span>);
            </span><span>closedir</span> ( <span>$handle</span><span> );

        }
    }

    </span><span>function</span> delDir(<span>$path</span> = ''<span>)
    {
        </span><span>if</span> (<span>empty</span>(<span>$path</span><span>)) {

            </span><span>echo</span> '请指定要操作的文件路径'<span>;
            </span><span>return</span> <span>false</span><span>;

        } </span><span>else</span><span> {

            </span><span>$path</span> = <span>str_replace</span>('\\', '/', <span>$path</span><span>);

        }

        </span><span>if</span> ( <span>$handle</span> = <span>opendir</span>(<span>$path</span><span>)) {

            </span><span>while</span> (<span>false</span> !== ( <span>$fileName</span>  =  <span>readdir</span> ( <span>$handle</span><span> ))) {

                </span><span>if</span> ( <span>$fileName</span>  !=  "."  &&  <span>$fileName</span>  !=  ".."<span> ) {

                    </span><span>if</span> (<span>is_dir</span>(<span>$path</span> . '/' . <span>$fileName</span><span>)) {

                        </span><span>echo</span> <span>$fileName</span> . "<br />"<span>;

                        </span><span>//</span><span> 删除含有Zip字符的目录</span>
                        <span>if</span> (<span>strpos</span>(<span>$fileName</span>, 'Zip') !== <span>false</span><span>) {

                            delFile(</span><span>$path</span> . '/' . <span>$fileName</span><span>);
                            
                        } </span><span>else</span><span> {

                            delDir(</span><span>$path</span> . '/' . <span>$fileName</span><span>);

                        }

                    }

                }

            }

            </span><span>closedir</span> ( <span>$handle</span><span> );

        }

    }
    
    </span><span>delDir('D:\web\Apache\htdocs\KeyShareMall\Pc\ThinkPHP');</span>

 ?></span>
Copy after login

以上就介绍了PHP删除符合条件的整个目录,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Steps to set the PATH environment variable of the Linux system Steps to set the PATH environment variable of the Linux system Feb 18, 2024 pm 05:40 PM

How to set the PATH environment variable in Linux systems In Linux systems, the PATH environment variable is used to specify the path where the system searches for executable files on the command line. Correctly setting the PATH environment variable allows us to execute system commands and custom commands at any location. This article will introduce how to set the PATH environment variable in a Linux system and provide detailed code examples. View the current PATH environment variable. Execute the following command in the terminal to view the current PATH environment variable: echo$P

How to set the path environment variable How to set the path environment variable Sep 04, 2023 am 11:53 AM

Method to set the path environment variable: 1. Windows system, open "System Properties", click the "Properties" option, click "Advanced System Settings", in the "System Properties" window, select the "Advanced" tab, and then click "Environment Variables" " button, find and click "Path" to edit and save; 2. For Linux systems, open the terminal, open your bash configuration file, add "export PATH=$PATH: file path" at the end of the file and save it; 3. For MacOS system, the operation is the same as above.

How to correctly set the PATH environment variable in Linux How to correctly set the PATH environment variable in Linux Feb 22, 2024 pm 08:57 PM

How to correctly set the PATH environment variable in Linux In the Linux operating system, environment variables are one of the important mechanisms used to store system-level configuration information. Among them, the PATH environment variable is used to specify the directories in which the system searches for executable files. Correctly setting the PATH environment variable is a key step to ensure the normal operation of the system. This article will introduce how to correctly set the PATH environment variable in Linux and provide specific code examples. 1. Check the current PATH environment variable and enter the following command in the terminal

How to configure path environment variable in java How to configure path environment variable in java Nov 15, 2023 pm 01:20 PM

Configuration steps: 1. Find the Java installation directory; 2. Find the system environment variable settings; 3. In the environment variable window, find the variable named "Path" and click the edit button; 4. In the pop-up edit environment variable window , click the "New" button, and enter the Java installation path in the pop-up dialog box; 5. After confirming that the input is correct, click the "OK" button.

The role and importance of the PATH environment variable in Linux The role and importance of the PATH environment variable in Linux Feb 21, 2024 pm 02:09 PM

"The Role and Importance of the PATH Environment Variable in Linux" The PATH environment variable is one of the very important environment variables in the Linux system. It defines which directories the system searches for executable programs. In the Linux system, when the user enters a command in the terminal, the system will search one by one in the directories listed in the PATH environment variable to see if the executable file of the command exists. If found, it will be executed. Otherwise, "commandnotfound" will be prompted. The role of the PATH environment variable: Simplified

How to configure path in java environment variables How to configure path in java environment variables Apr 22, 2023 pm 06:49 PM

1. Find the bin directory under the jdk installation directory and copy it. 2. Click Computer and select Properties; 3. Select Advanced, Environment Variables; 4. Paste at the path line. Note the English half-width symbol (;) at the end. Administrator user variables are only for Used by the administrator user, system variables can be used by all users. Among environment variables, path is used to ensure that java commands are executed under the path. It can be said to be an indispensable link in environment variable configuration.

How to use file separator and path separator in Java? How to use file separator and path separator in Java? Apr 21, 2023 pm 02:40 PM

1. File delimiters File delimiters are characters used to separate directory names that constitute a path to a specific location. 1. Get the file delimiter In Java, there are several ways to get the file delimiter. We can use File.separator to get the separator of String. StringfileSeparator=File.separator; We can also use File.separatorChar to obtain the character of this separator. charfileSeparatorChar=File.separatorChar; Starting from Java7, we can also use the file system. StringfileSeparator=F

File system operations using the new Files and Path classes in Java 11 File system operations using the new Files and Path classes in Java 11 Jul 30, 2023 pm 10:25 PM

File system operations using the new Files and Path classes in Java 11 Java 11 introduces a number of new classes and features, including improvements to file system operations. The new Files class and Path class provide a more convenient and flexible file operation method, allowing us to process files and directories more easily. Before Java11, we usually used the java.io.File class to operate files and directories. However, the functionality of this class is relatively limited and not flexible enough. Java

See all articles