Maison > développement back-end > tutoriel php > PHP剔除符合条件的整个目录

PHP剔除符合条件的整个目录

WBOY
Libérer: 2016-06-13 12:18:03
original
859 Les gens l'ont consulté

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

<span style="font-size: 15px;"><span style="color: #000000;">php    </span><span style="color: #008000;">/*</span><span style="color: #008000;">*    *    @name       delFile函数与delDir函数一起使用, 删除符合条件的整个目录    *    @param      string  $path   指定操作路径    *    @return     null    *    @example    delDir('D:\web\Apache\htdocs\KeyShareMall\Pc\ThinkPHP');    </span><span style="color: #008000;">*/<br>  <br></span>   // 删除目录    <span style="color: #0000ff;">function</span> delFile(<span style="color: #800080;">$path</span><span style="color: #000000;">)    {        </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">)) {            </span><span style="color: #0000ff;">echo</span> '请指定要操作的文件路径'<span style="color: #000000;">;            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;        }        </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$handle</span>  =  <span style="color: #008080;">opendir</span> ( <span style="color: #800080;">$path</span><span style="color: #000000;"> )) {            </span><span style="color: #0000ff;">while</span> ( <span style="color: #0000ff;">false</span>  !== ( <span style="color: #800080;">$fileName</span>  =  <span style="color: #008080;">readdir</span> ( <span style="color: #800080;">$handle</span><span style="color: #000000;"> ))) {                </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$fileName</span>  !=  "."  &&  <span style="color: #800080;">$fileName</span>  !=  ".."<span style="color: #000000;"> ) {                                        </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_file</span>(<span style="color: #800080;">$path</span> . '/' . <span style="color: #800080;">$fileName</span><span style="color: #000000;">)) {                        </span><span style="color: #008080;">unlink</span>(<span style="color: #800080;">$path</span> . '/' . <span style="color: #800080;">$fileName</span><span style="color: #000000;">);                                                                    }                    </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_dir</span>(<span style="color: #800080;">$path</span> . '/' . <span style="color: #800080;">$fileName</span><span style="color: #000000;">)) {                        delFile(</span><span style="color: #800080;">$path</span> . '/' . <span style="color: #800080;">$fileName</span><span style="color: #000000;">);                                            }                }            }            </span><span style="color: #008080;">rmdir</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">);            </span><span style="color: #008080;">closedir</span> ( <span style="color: #800080;">$handle</span><span style="color: #000000;"> );        }    }    </span><span style="color: #0000ff;">function</span> delDir(<span style="color: #800080;">$path</span> = ''<span style="color: #000000;">)    {        </span><span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">)) {            </span><span style="color: #0000ff;">echo</span> '请指定要操作的文件路径'<span style="color: #000000;">;            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;        } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {            </span><span style="color: #800080;">$path</span> = <span style="color: #008080;">str_replace</span>('\\', '/', <span style="color: #800080;">$path</span><span style="color: #000000;">);        }        </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$handle</span> = <span style="color: #008080;">opendir</span>(<span style="color: #800080;">$path</span><span style="color: #000000;">)) {            </span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">false</span> !== ( <span style="color: #800080;">$fileName</span>  =  <span style="color: #008080;">readdir</span> ( <span style="color: #800080;">$handle</span><span style="color: #000000;"> ))) {                </span><span style="color: #0000ff;">if</span> ( <span style="color: #800080;">$fileName</span>  !=  "."  &&  <span style="color: #800080;">$fileName</span>  !=  ".."<span style="color: #000000;"> ) {                    </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">is_dir</span>(<span style="color: #800080;">$path</span> . '/' . <span style="color: #800080;">$fileName</span><span style="color: #000000;">)) {                        </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$fileName</span> . "<br>"<span style="color: #000000;">;                        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 删除含有Zip字符的目录</span>                        <span style="color: #0000ff;">if</span> (<span style="color: #008080;">strpos</span>(<span style="color: #800080;">$fileName</span>, 'Zip') !== <span style="color: #0000ff;">false</span><span style="color: #000000;">) {                            delFile(</span><span style="color: #800080;">$path</span> . '/' . <span style="color: #800080;">$fileName</span><span style="color: #000000;">);                                                    } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {                            delDir(</span><span style="color: #800080;">$path</span> . '/' . <span style="color: #800080;">$fileName</span><span style="color: #000000;">);                        }                    }                }            }            </span><span style="color: #008080;">closedir</span> ( <span style="color: #800080;">$handle</span><span style="color: #000000;"> );        }    }        </span><span style="color: #008000;">delDir('D:\web\Apache\htdocs\KeyShareMall\Pc\ThinkPHP');</span> ?></span>
Copier après la connexion

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal