PHP SPL Directoryiterator How to get a list of site directories _php tutorial

Source: Internet
Author: User
Tags spl rewind website server

PHP SPL Directoryiterator How to get a list of site directories


Since PHP 5.0 PHP has added a number of built-in components and interfaces, such as SPL (Standard PHP library, full name: standardized PHP libraries), the library defines a number of classes and interfaces greatly facilitates PHP developers to quickly and easily develop the actual project. This article will briefly record a class about folder (directory) processing in the SPL library, Directoryiterator.

About processing folders (directories), first look at the traditional processing methods, you can see the article:

PHP reads directories and tables displays the functions of files in the directory

The following method is implemented with the Directoryiterator class as in the previous connection article:

function Listdir ($dir) {if (!file_exists ($dir) | |! Is_dir ($dir)) return '; $dir =new directoryiterator ($dir); $dirList =array (' Dirnum ' =>0, ' FileNum ' =>0, ' lists ' = > "), while ($dir->valid ()) {if ($dir->getfilename ()!== '. ' && $dir->getfilename ()!== ' ... ') {$dirList [' lists '] [$dir->key ()] [' Name ']= $dir->getfilename (); if ($dir->isdir ()) {//Determine if directory $dirlist[' Dirnum ']++; $dirList [' Lists '] [$dir->key ()] [' Isdir ']=1;} else{$dirList [' FileNum ']++; $dirList [' Lists '] [$dir->key ()] [' Isdir ']= ';}}    $dir->next ();    }    return $dirList;}

Here are some of the more common methods and explanations for the Directoryiterator class:

Method

Description

Directoryiterator::__construct

Build a new directory iterator from the path

Directoryiterator::current

Returns the current object (the method required by the iterator interface)

Directoryiterator::getatime

Get the last access time of the file

Directoryiterator::getctime

Get the Inode modification time of the file

Directoryiterator::getchildren

If this is a directory, returns an iterator for the current item

Directoryiterator::getfilename

Returns the file name of the current directory entry

Directoryiterator::getgroup

Get file Groups

Directoryiterator::getinode

Get the file Inode

Directoryiterator::getmtime

Get the last modified time for the file

Directoryiterator::getowner

Get the file owner

Directoryiterator::getpath

Return directory path

Directoryiterator::getpathname

Returns the path and file name of the current directory entry

Directoryiterator::getperms

Get file permissions

Directoryiterator::getsize

Get File size

Directoryiterator::gettype

Get file type

Directoryiterator::isdir

Returns true if the current item is a directory

Directoryiterator::isdot

Returns true if the current item is. OR:

Directoryiterator::isexecutable

Returns true if the file is executable

Directoryiterator::isfile

Returns true if the file is a regular file

Directoryiterator::islink

Returns true if the file is a symbolic link

Directoryiterator::isreadable

Returns true if the file is readable

Directoryiterator::iswritable

Returns true if the file is writable

Directoryiterator::key

Returns the current directory entry

Directoryiterator::next

Move to the next item

Directoryiterator::rewind

Return the directory pointer to the start position

Directoryiterator::valid

Check if the directory contains more items

In addition, the method of processing directories in the SPL class library has the Recursivedirectoryiterator method, the difference between this method and the Directoryiterator method is that all files including subdirectories can be obtained.

The methods for Recursivedirectoryiterator are:

Method

Description

Recursivedirectoryiterator::getchildren

If this is a directory, returns an iterator for the current item

Recursivedirectoryiterator::haschildren

Returns whether the current item is a directory instead of. Or.

Recursivedirectoryiterator::key

Returns the path and file name of the current directory entry

Recursivedirectoryiterator::next

Move to the next item

Recursivedirectoryiterator::rewind

Return the directory pointer to the start position

Recursiveiteratoriterator::current

Accessing the current element value

Recursiveiteratoriterator::getdepth

Get the current depth of the recursive iteration

Recursiveiteratoriterator::getsubiterator

Get the current active sub-iterator

Recursiveiteratoriterator::key

Access the current key

Recursiveiteratoriterator::next

Move forward to the next element

Recursiveiteratoriterator::rewind

Returns an iterator to the first element of a top-level inner iterator

Recursiveiteratoriterator::valid

Check if the current location is legal

As you can see, SPL can quickly and efficiently handle all the problems you might encounter with the directory.

Also refer to the PHP Class Library's standard documentation for the PHP Directoryiterator class:

http://php.net/manual/en/class.directoryiterator.php

Articles you may be interested in

    • Usage and differences in PHP that jump out of multiple loops using Break,continue,goto,return,exit
    • The usage and difference of echo,print,print_r,var_export,var_dump in PHP
    • Dedecms (Dream Weaving) website Server directory security Settings experience sharing
    • How to remove the index.php string from the URL of the website CodeIgniter developed
    • Comparison of performance efficiency of file_get_contents to curl in PHP
    • PHP compressed HTML page code (clear spaces, line breaks, tabs, comment markers)
    • PHP reads directories and tables displays the functions of files in the directory
    • Use PHP function memory_get_usage to get the current PHP memory consumption to achieve program performance optimization

http://www.bkjia.com/PHPjc/992744.html www.bkjia.com true http://www.bkjia.com/PHPjc/992744.html techarticle PHP SPL Directoryiterator methods to get a list of site directories since PHP 5.0 PHP has added some built-in components and interfaces, such as SPL (standard PHP library, full name: PHP libraries) ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.