php 目录及文件操作

// bool is_dir(string $filename) 判断给定文件名是否是一个目录。
// resource opendir(string $path[,resource $context]) // 打开一个目录句柄,可用于之后的 closedir(),readdir() 和 rewinddir() 调用中。$path 要打开的目录
// string readdir([resource $dir_handler]) // $dir_handler 目录句柄,用opendir打开的 ,返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回。
// void closedir(resource $dir_handler) // 关闭目录句柄, 由 opendir打开的
$base_dir = "test/";
if(is_dir($base_dir))
{
    $fos = opendir($base_dir); //

    while($list = readdir($fos)) //
    {
        echo $list . "<br />";
    }
    closedir($fos); //
}
*/

// string dirname(string $path) // 给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名。
// $path 一个路径。在 Windows 中,斜线(/)和反斜线()都可以用作目录分隔符。在其它环境下是斜线(/)。
// 返回 path 的父目录。 如果在 path 中没有斜线,则返回一个点('.'),表示当前目录。否则返回的是把 path 中结尾的 /component(最后一个斜线以及后面部分)去掉之后的字符串。

//string basename(string $path[, string $suffix]); // 给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名
// 如果文件名是以 suffix 结束的,那这一部分也会被去掉。

// string realpath(string $path) // 返回规范化的绝对路径名,会自动转化为 开如 d:/www/xx.php
// $path 要检查的路径。

// mix pathinfo(string $path[, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ]) 返回文件路径信息
//形如:$str['dirname'] $str['basename'] $str['extension'] $str['filename']

// 创建目录 bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
// $pathname: 目录路径 $mode: 默认的 mode 是 0777,意味着最大可能的访问权
 // bool  rmdir($path[, resource $context]); // 尝试删除 dirname 所指定的目录。 该目录必须是空的,而且要有相应的权限。 失败时会产生一个 E_WARNING 级别的错误。

// 是以面向对象的方式来读取
// directory dir(string $directory[, resourct $content]); 返回一个  Directory 类实例,以面向对象的方式访问目录。打开 directory 参数指定的目录。 , $content 对上下文(Context)的支持

// $dir->read([resource $dir_handle]) // 从目录句柄中读取条目
// $dir->close(); //关闭
/*
$d = dir('./test');

echo $d->handle . "<br />";
while($dd = $d->read())
{
    echo $dd . "<br />";
}
$d->close();
*/

// =文件操作
// bool is_readable(string $filename) // 判定给定文件名是否可读
/*
$file = './test/ok.php';
if(is_readable($file))
{
    echo 'yes';
}else
{
    echo 'No';
}
*/

// bool is_writeable(string $filename) // 判断给定的文件名是否可写
/*
$file = './test/ok.php';
if(is_writeable($file))
{
    echo 'yes';
}else
{
    echo 'No';
}
*/
// bool flock ( resource $handle , int $operation [, int &$wouldblock ] ); 对文件操作时进行锁定
/*
handle

    文件系统指针,是典型地由 fopen() 创建的 resource(资源)。
operation

    operation 可以是以下值之一:

        LOCK_SH取得共享锁定(读取的程序)。
        LOCK_EX 取得独占锁定(写入的程序。
        LOCK_UN 释放锁定(无论共享或独占)。

    如果不希望 flock() 在锁定时堵塞,则是 LOCK_NB(Windows 上还不支持)。
wouldblock

    如果锁定会堵塞的话(EWOULDBLOCK 错误码情况下),可选的第三个参数会被设置为 TRUE。(Windows 上不支持)
        */

        // unlink(string $filename[,resource $context]) // 删除指定文件
        /*
        $bas = './test/tt.html';
        unlink($bas);
        */
    //    bool copy(string $oldfile, string $newold[, resource $context])//复制文件
    // 文件存在,将被覆盖
// int filemtime(string $filename) // 取得文件修改时间,本函数返回文件中的数据块上次被写入的时间,也就是说,文件的内容上次被修改的时间。 返回文件上次被修改的时间, 或者在失败时返回 FALSE。时间以 Unix 时间戳的方式返回,可用于 date()。

// int filesize(string $filename); // 取得指定文件大小,返回文件大小的字节数,

另外,目录的相对与绝对路径如下:

|

|

test/test.php

|

|

index.php

在test.php中,代码如下 define('MY_DIR', dirname(__FILE__)); // MY_DIR = 路径/test

index.php中,代码如下:

require('./test/test.php');

echo MY_DIR; // 路径/test 与 test.php 中定义的值一样

原文地址:https://www.cnblogs.com/lin3615/p/3600110.html