php Directory

php directory 函数学习:

 

The Directory Class

Introdution

instances of directory are created by calling dir() function,not by the new operator

Class synopsis

     Directory {

         properties

          public string $path

           public resource $handle

           methods

           public void close([resource $dir_handle])

           public string read([resource $dir_handle])

          public void rewind([resource $dir_handle])

      }

Properties

$path: the directory that are opened

$handle can be used readdir closedir rewinddir

Table of contents

Directory::close ---close directory handle

Directory::read ----read entry from directory handle

Directory::rewind ---rewind directory handle

dir--return a instance of directory class

description

directory dir(string $directory [,resource $context])

a pseudo-object oriented mechanism for reading a directory.The given directory is opened.

parameters

$directory

the directory to open

$context

Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Streams.

return values

returns a instance of dirctory class, or null with wrong parameters, or false in case of anyother error

examples

<?php
    $dir = dir('test');
    echo $dir->path .'<br />';
    echo $dir->handle.'<br />';
    while($file = $dir->read()){
        echo $file.'<br />';
    }
    $dir->close();
?>
output

.
..
a
ab
b



is_dir---tells whether the filename is a directory

description

bool is_dir(string $filename)

Tells whether the given filename is a directory

parameters

$filename

Path to the file. If filename is a relative filename, it will be checked relative to the current working directory.

If filename is a symbolic or hard link then the link will be resolved and checked. If you have enabled safe mode, or open_basedir further restrictions may apply

return values

Returns TRUE if the filename exists and is a directory, FALSE otherwise

errors/exceptions

Upon failure, an E_WARNING is emitted

Note

The results of this function are cached. See clearstatcache() for more details.

Tip

As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

examples

<?php
    var_dump(is_dir('test.txt'));
    var_dump(is_dir('test'));
?>

output

bool(false) 
bool(true)


mkdir---makes directory

description

bool mkdir(string $pathname[,int $mode=0777[,int $recursive=false[,resource $context]]])

Attempts to create the directory specified by pathname

parameters

$pathname

the directory path

$mode

The mode is 0777 by default, which means the widest possible access. For more information on modes, read the details on the chmod() page

Note: mode is ignored on Windows. 

$recursive

Allows the creation of nested directories specified in the pathname

$context

Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Streams

return values

return true on success or falsse on failure

examplets

<?php
    // Desired folder structure
    $structure = 'a/b/c';

    // To create the nested structure, the $recursive parameter 
    // to mkdir() must be specified.

    if (!mkdir($structure, 0, true)) {
        die('Failed to create folders...');
    }
?>



 rmdir---remove directory

 description

  bool rmdir(string $dirname[,resource $context])

  Attempts to remove the directory named by dirname. 

The directory must be empty, and the relevant permissions must permit this. A E_WARNING level error will be generated on failure.

 parameters

  $dirname

      path to the directory

  $context

      Note: Context support was added with PHP 5.0.0. For a description of contexts, refer to Streams.

 Notes

      Note: When safe mode is enabled, PHP checks whether the directory in which the script is operating has the same UID (owner) as the script that is being executed.

 Changelog

      Version Description 

      5.0.0 As of PHP 5.0.0 rmdir() can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers for a listing of which wrappers support rmdir().  

 examples

<?php
    if (!is_dir('abc')) {
        mkdir('abc');
    }

    rmdir('abc');
?>


getcwd—gets the current working directory

description

string getcwd(void)

return values

return the current working directory on success or false on failure

on some unix variants,getcwd will return false if any one of parent directories does not have readable or search mode set,even if the current directory does;

examples

<?php
    echo getcwd();
?>

output

E:\work\try



chdir—change directory

description:

boole chdir(string $directory)

changes current directory to $directory

parameters

$directory

the new current directory

return values

returns true on success or false on failure

errors/exceptions

throws an error of level e_warning on failure

examples

<?php
    echo getcwd().'<br />';
    chdir('test');
    echo getcwd();

?>

output

E:\work\try 
E:\work\try\test


opendir---open directory handle

description

resource opendir(string $directory[, resource $context])

opens up a directory handle to be used in subsequent readdir(),closedir(),rewinddir() calls.

parameters

$directory

the directory path that to be opened

$context

For a description of the context parameter, refer to the streams section of the manual. 

return values

return a directory handle resource on success,or false on failurel;

if path is not a valid directory or the directory can be not opened due to permission restrictions or filesystem error,

opendir() returns false and generates a PHP error of level e_warning. You can supress the error output of opendir() by prepending @ to the front of function name.

change log
version                                                             description
5.0.0       path supports the ftp:// URL wrapper
4.3.0         path can also be any URL which supports directory listing, however only the file:// URL wrapper supports this in PHP 4 
examples

<?php
     if(is_dir('test')){
        $dir = @opendir('test');
        while(($file = readdir($dir)) !== false){
            echo $file.'<br />';
        }
        closedir($dir);
    }
?>


readdir---read entry from directory handle

description

string readdir(resource $dir_handle)

returns the name of in the next entry in the directory,the entries are returned in the order in which they are stored by the FilesystemIterator

parameters

$dir_handle

The directory handle resource previously opened with opendir(). If the directory handle is not specified, the last link opened by opendir() is assumed. 

return values

return the entry name on success or null on failure

        Warning

        This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

examples

the same to opendir()


closedir---close the directory handle

description

void closedir([resource $dir_handle])

Closes the directory stream indicated by dir_handle. The stream must have previously been opened by opendir(). 

paramter

$dir_handle

The directory handle resource previously opened with opendir(). If the directory handle is not specified, the last link opened by opendir() is assumed. 

examples

the same to opendir()


rewinddir----rewind directory handle

description

void rewinddir(resource $dir_handle)

 Resets the directory stream indicated by dir_handle to the beginning of the directory

parameters

$dir_handle

The directory handle resource previously opened with opendir(). If the directory handle is not specified, the last link opened by opendir() is assumed


scandir--List files and directories inside the specified path

description

array scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING [, resource $context ]] )

returns an array of files and directories from the $directory

parameters

$directory

the directory will be scanned

        $sorting_order

By default, the sorted order is alphabetical in ascending order. If the optional sorting_order is set to SCANDIR_SORT_DESCENDING, then the sort order is alphabetical in descending order. If it is set to SCANDIR_SORT_NONE then the result is unsorted. 

        $context

For a description of the context parameter, refer to the streams section of the manual. 

return values

Returns an array of filenames on success, or FALSE on failure. If directory is not a directory, then boolean FALSE is returned, and an error of level E_WARNING is generated

examples

<?php
    print_r(scandir('test'));    
    print_r(scandir('test',1));
?>

output
    Array ( [0] => . [1] => .. [2] => a [3] => abc [4] => b )
    Array ( [0] => b [1] => abc [2] => a [3] => .. [4] => . )


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值