Rumah > pembangunan bahagian belakang > tutorial php > PHP删除符合条件的整个目录,符合条件整个目录_PHP教程

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

WBOY
Lepaskan: 2016-07-13 09:57:26
asal
845 orang telah melayarinya

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>
Salin selepas log masuk

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/983850.htmlTechArticlePHP删除符合条件的整个目录,符合条件整个目录 ? php /* * * @name delFile函数与delDir函数一起使用, 删除符合条件的整个目录 * @param string $path...
Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
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
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan