Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP删除符合条件的整个目录

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

WBOY
WBOYasal
2016-08-08 09:25:54841semak imbas

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

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

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn