filestat
可以返回一系列系统的文件状态,如是否存在,文件或者目录,是否可读写等。
// 引入头文件
#include "ext/standard/php_filestat.h"
// 假设我们要检查的文件为 /tmp/foo.txt
zend_string *filename = zend_string_init("/tmp/foo.txt", sizeof("/tmp/foo.txt") - 1, 0);
int funcnum = FS_IS_DIR; // 这里描述检查类型,FS_IS_DIR 是检查是否为文件夹,与 is_dir() PHP 方法相同
zval ret; // FS_IS_DIR 返回的结果其实为一个布尔类型的 zval
php_stat(ZSTR_VAL(filename), (php_stat_len) ZSTR_LEN(filename), funcnum, &ret);
if (Z_TYPE(ret) == IS_TRUE) {
// 目录
} else {
// 不是目录
}
以下是 funcnum
宏值和 PHP 方法的对照,来自 php_filestat.h
的定义
#define FS_PERMS 0 // int fileperms(string filename)
#define FS_INODE 1 // int fileinode(string filename)
#define FS_SIZE 2 // int filesize(string filename)
#define FS_OWNER 3 // int fileowner(string filename)
#define FS_GROUP 4 // int filegroup(string filename)
#define FS_ATIME 5 // int fileatime(string filename)
#define FS_MTIME 6 // int filemtime(string filename)
#define FS_CTIME 7 // int filectime(string filename)
#define FS_TYPE 8 // string filetype(string filename)
#define FS_IS_W 9 // bool is_writable(string filename)
#define FS_IS_R 10 // bool is_readable(string filename)
#define FS_IS_X 11 // bool is_executable(string filename)
#define FS_IS_FILE 12 // bool is_file(string filename)
#define FS_IS_DIR 13 // bool is_dir(string filename)
#define FS_IS_LINK 14 // bool is_link(string filename)
#define FS_EXISTS 15 // bool file_exists(string filename)
#define FS_LSTAT 16 // array lstat(string filename)
#define FS_STAT 17 // array stat(string filename)
网友评论