streamWrapper::stream_stat()

获取文件的状态信息

首字母:S

函数名称:streamWrapper::stream_stat()

函数描述:streamWrapper::stream_stat() 函数用于获取文件的状态信息。

适用版本:PHP 5, PHP 7

语法:mixed streamWrapper::stream_stat(string $path, int $flags)

参数:

  • $path:要获取状态信息的文件路径。
  • $flags:可选参数,用于指定额外的标志。默认为 0。

返回值:返回一个包含文件状态信息的关联数组,或者在失败时返回 false。

用法示例:

position = 0;
 $this->data = "Hello, World!";
 }
 public function stream_open($path, $mode, $options, &$opened_path) {
 return true;
 }
 public function stream_stat($path, $flags) {
 $stat = array(
 'dev' => 0,
 'ino' => 0,
 'mode' => 33206, // 文件权限:0644
 'nlink' => 0,
 'uid' => 0,
 'gid' => 0,
 'rdev' => 0,
 'size' => strlen($this->data), // 文件大小
 'atime' => time(), // 访问时间
 'mtime' => time(), // 修改时间
 'ctime' => time(), // 创建时间
 'blksize' => -1,
 'blocks' => -1,
 );
 return $stat;
 }
}
// 注册自定义流处理器
stream_wrapper_register('mywrapper', 'MyStreamWrapper');
// 使用自定义流处理器打开文件
$file = fopen('mywrapper://test.txt', 'r');
// 获取文件状态信息
$stat = stream_stat($file);
var_dump($stat);
?>

输出:

array(13) {
 ["dev"]=>
 int(0)
 ["ino"]=>
 int(0)
 ["mode"]=>
 int(33206)
 ["nlink"]=>
 int(0)
 ["uid"]=>
 int(0)
 ["gid"]=>
 int(0)
 ["rdev"]=>
 int(0)
 ["size"]=>
 int(13)
 ["atime"]=>
 int(1638448778)
 ["mtime"]=>
 int(1638448778)
 ["ctime"]=>
 int(1638448778)
 ["blksize"]=>
 int(-1)
 ["blocks"]=>
 int(-1)
}

以上示例展示了如何使用自定义流处理器(MyStreamWrapper)来打开文件,并使用 stream_stat() 函数获取文件的状态信息。在自定义流处理器的 stream_stat() 方法中,我们返回了一个包含文件状态信息的关联数组,其中包括了文件的权限、大小、访问时间、修改时间和创建时间等信息。最后,我们通过 var_dump() 函数打印出获取到的文件状态信息。

同首字母函数