兼容PHP5的PHP目录管理函数库

2015-01-24信息快讯网

php下进行目录的一些操作,经常用到的方法

主要能兼容: PHP 5
一、chdir -- 改变目录
语法:bool chdir ( string directory )
返回值:整数
函数种类: 文件存取
内容说明:
将 PHP 的当前目录改为directory。directory:新的当前目录。返回值如果成功则返回 TRUE,失败则返回 FALSE。
例子讲解: 

 程序代码
<?php
// current directory
echo getcwd() . "\n";
chdir('public_html');
// current directory
echo getcwd() . "\n";
?>


输出结果为:
/home/vincent
/home/vincent/public_html

注意:循环语句中会出现“ Warning: chdir(): No such file or directory (errno 2) in ***** on line *”错误。


 程序代码
<?php
// current directory
echo getcwd() . "\n";
for($i=1; $i<=2; $i++){
chdir('whoist');
// current directory
echo getcwd() . "\n";
}
?>


二、dir -- directory 类
语法:new dir(string directory);
返回值:类
函数种类: 文件存取
内容说明:
这是一个类似面向对象的类别类,用来读取目录。当目录参数 directory 打开之后,有二个属性可用:handle 属性就像其它非类的函数所用的 readdir()、rewinddir() 及 closedir();path 属性则配置打开目录后的路径参数。本类有三个方法 (method):read、rewind 与 close。
class dir {
dir ( string directory )
string path
resource handle
string read ( void )
void rewind ( void )
void close ( void )
}
例子讲解: 

 程序代码
<?php
$d = dir("/etc/php5");
echo "Handle: " . $d->handle . "\n";
echo "Path: " . $d->path . "\n";
while (false !== ($entry = $d->read())) {
echo $entry."\n"; 
}
$d->close();
?>


输出结果为:
Handle: Resource id #2
Path: /etc/php5
.
..
apache
cgi
cli

注: read 方法返回的目录项的顺序依赖于系统。
注: 本函数定义了内部类 Directory,意味着不能再用同样的名字定义用户自己的类。

三、closedir -- 关闭目录句柄
语法:void closedir ( resource dir_handle )
返回值:无
函数种类: 文件存取
内容说明:
关闭由 dir_handle 指定的目录流。流必须之前被 opendir() 所打开。
例子讲解:

 程序代码
<?php
$dir = "/etc/php5/";
// Open a known directory, read directory into variable and then close
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        $directory = readdir($dh);
        closedir($dh);
    }
}
?>


四、opendir -- 打开目录句柄
语法:resource opendir ( string path [, resource context] )
返回值:整数
函数种类: 文件存取
内容说明:
本函数用来打开目录资料流。返回的整数是可供其它目录函数closedir(),readdir() 和 rewinddir() 操作的 handle。如果成功则返回目录句柄的resource,失败则返回 FALSE。
例子讲解:

 程序代码
<?php
$dir = "/etc/php5/";
// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
       }
        closedir($dh);
    }
}
?>


输出结果为:
filename: . : filetype: dir
filename: .. : filetype: dir
filename: apache : filetype: dir
filename: cgi : filetype: dir
filename: cli : filetype: dir
简单的PHP留言本实例代码
php 上一篇,下一篇文章实现代码与原理说明
php 获取本机外网/公网IP的代码
php 仿Comsenz安装效果代码打包提供下载
php ob_flush,flush在ie中缓冲无效的解决方法
php 函数使用方法与函数定义方法
PHP5中使用DOM控制XML实现代码
PHP 面向对象 PHP5 中的常量
一些被忽视的PHP函数(简单整理)
PHP 字符串加密函数(在指定时间内加密还原字符串,超时无法还原)
PHP GD 图像处理组件的常用函数总结
PHP PDO函数库详解
PHP5 字符串处理函数大全
PHP5与MySQL数据库操作常用代码 收集
libmysql.dll与php.ini是否真的要拷贝到c:\windows目录下呢
php5 non-thread-safe和thread-safe这两个版本的区别分析
PHP 创建文件(文件夹)以及目录操作代码
php is_file()和is_dir()用于遍历目录时用法注意事项
Search File Contents PHP 搜索目录文本内容的代码
PHP 获取目录下的图片并随机显示的代码
php下实现一个阿拉伯数字转中文数字的函数
php表单提交程序的安全使用方法第1/2页
php 防止单引号,双引号在接受页面转义
功能齐全的PHP发送邮件类代码附详细说明
使用PHP批量生成随机用户名
用PHP与XML联手进行网站编程代码实例
php5中date()得出的时间为什么不是当前时间的解决方法
php 在线打包_支持子目录
js下函数般调用正则的方法附代码
php的正则处理函数总结分析
PHP伪静态页面函数附使用方法
php utf-8转unicode的函数第1/2页
php学习 函数 课件
php创建多级目录代码
WindowsXP中快速配置Apache+PHP5+Mysql
php5数字型字符串加解密代码
PHP下用rmdir实现删除目录的三种方法小结
php5 mysql分页实例代码
PHP5中的时间相差8小时的解决办法
php模板之Phpbean的目录结构
PHP读取目录下所有文件的代码
©2014-2024 dbsqp.com