Home >Backend Development >PHP Tutorial >PHP deletes the entire directory that meets the conditions, the entire directory that meets the conditions_PHP tutorial

PHP deletes the entire directory that meets the conditions, the entire directory that meets the conditions_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:57:26835browse

PHP删除符合条件的整个目录,符合条件整个目录

<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>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/983850.htmlTechArticlePHP删除符合条件的整个目录,符合条件整个目录 ? php /* * * @name delFile函数与delDir函数一起使用, 删除符合条件的整个目录 * @param string $path...
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