PHP Directory 函数的详解

yipeiwu_com6年前PHP代码库

预定义常量:

DIRECTORY_SEPARATOR (string) :目录分隔符

PATH_SEPARATOR (string) :路径分隔符


bool chdir ( string $directory )— 改变目录

复制代码 代码如下:

 echo getcwd() . "\n";
 chdir('public_html');
 echo getcwd() . "\n";


bool chroot ( string $directory )— 改变根目录,仅在系统支持且运行于 CLI,CGI 或嵌入 SAPI 版本时才行。

dir::dir ( string $directory )— directory 类,有三个方法可用:read,rewind(将文件内部的位置指针重新指向一个数据流开头) 和 close

复制代码 代码如下:

$d = dir("E:/work/html");
 foreach($d as $k=>$v){
     echo $k.'->' .$v. '<br/>';
 }
 while(false !== ($entry = $d->read())){
     echo $entry."<br/>";
 }
 $d->close();
 

 void closedir ( resource $dir_handle )— 关闭目录句柄

复制代码 代码如下:

$dir = "/etc/php5/";

 if (is_dir($dir)) {
     if ($dh = opendir($dir)){
         $directory = readdir($dh);
         closedir($dh);
     }
 }
 

 string getcwd ( void )— 取得当前工作目录

resource opendir ( string $path [, resource $context ] )— 打开目录句柄

string readdir ( resource $dir_handle )— 从目录句柄中读取条目

复制代码 代码如下:

if ($handle = opendir('/path/to/files')) {
     echo "Directory handle: $handle\n";
     echo "Files:\n";
     while (false !== ($file = readdir($handle))) {
         echo "$file\n";
     }
     closedir($handle);
 }

void rewinddir ( resource $dir_handle ) —将 dir_handle 指定的目录流重置到目录的开头

array scandir ( string $directory [, int $sorting_order [, resource $context ]] )— 列出指定路径中的文件和目录

复制代码 代码如下:

 $dir    = '/tmp';
 $files1 = scandir($dir);
 $files2 = scandir($dir, 1);
 print_r($files1);
 print_r($files2);

相关文章

PHP中if和or运行效率对比

本文实例讲述了PHP中if和or运行效率对比。分享给大家供大家参考。具体实现方法如下: 对if和or的运行效率进行了实例说明,感兴趣的朋友可以测试一下,这里我测试了的结果是or 比if效...

一个不易被发现的PHP后门代码解析

偶然间看到一段,看起来似乎没有什么问题,确是能致命的后门代码,这里用到了一个一般的PHPer都不怎么关注的反撇号 ` ,反撇号包含的字符串,等同于shell_exec函数。 伪装性很好,...

php检查字符串中是否有外链的方法

本文实例讲述了php检查字符串中是否有外链的方法。分享给大家供大家参考。具体实现方法如下: /** * is_external_link 检测字符串是否包含外链 * @param...

php将会员数据导入到ucenter的代码

我们要用的会员表结构 复制代码 代码如下: create table if not exists `net_111cnnet` ( `id` int(11) not null auto_...

PHP获取当前页面完整URL的实现代码

javascript实现:复制代码 代码如下:top.location.href   顶级窗口的地址 this.location.href  当前窗口的地址...