PHP löscht das gesamte Verzeichnis, das den Kriterien entspricht

WBOY
Freigeben: 2016-08-08 09:25:54
Original
847 Leute haben es durchsucht

<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>
Nach dem Login kopieren

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

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage