文件操作(splfileinfo和SplFileObject)的PHPSPL标准库实例

文件操作(splfileinfo和SplFileObject)的PHPSPL标准库实例
在PHP SPL,两类splfileinfo和splfileobject提供处理文件操作。

splfileinfo是用来获取文件的细节:

复制代码代码如下所示:

文件大小=新splfileinfo('foo-bar .txt);
print_r(阵列(

getatime= >文件> getatime(), / /上次访问时间

getbasename= >文件> getbasename),(basename /没有访问路径

getctime= >文件> getctime)、inode( / /修改时间

getextension= >文件> getextension(), / /文件扩展名

的作用:取文件名= >文件>作用:取文件名),( / /获取文件名

getgroup= >文件> getgroup(), / /获取文件组

getinode= >文件> getinode)、inode( / /获取文件

getlinktarget= >文件> getlinktarget),( / /获取文件链接的目标文件

getmtime= >文件> getmtime),( / /得到的最后修改时间

getOwner ' = >文件-> getOwner(), / /所有者

的方法:获取文件名路径= >文件>方法:获取文件名路径(), / /文件路径的文件名

getpathinfo= >文件> getpathinfo(),splfileinfo / /最优路径

getpathname= >文件> getpathname(), / /全路径

getperms= >文件> getperms(), / /文件权限

getrealpath= >文件> getrealpath(),绝对路径/文件

GetSize ' = >文件-> GetSize(), / /文件大小的字节数

方法= >文件-> GetType(文件目录),链接 / /文件类型

isdir= >文件> isdir(), / /目录

文件= >文件->文件(), / /文件

IsLink= >文件-> IsLink(),是 / /快速链接

isexecutable= >文件> isexecutable(), / /执行

isreadable= >文件> isreadable(), / /可读

iswritable= >文件> iswritable()可以写, / /

));
splfileobject继承splfileinfo实现recursiveiterator,文件遍历,查找seekableiterator界面和操作

穿越:

复制代码代码如下所示:

{试

foreach(新splfileobject('foo-bar .txt)达线){

回波$线;

}

catch(异常E){ }

echo $ E -> GetMessage();

}
查找指定行:

复制代码代码如下所示:

{试

文件大小=新splfileobject('foo-bar .txt);

$文件>查找(2);

当前文件;

catch(异常E){ }

echo $ E -> GetMessage();

}
写入CSV文件:

复制代码代码如下所示:

数组=

阵列('AAA',{BBB},CCC,'dddd),

阵列('123 ','456 ','7891),

数组()

);
文件大小=新splfileobject(CSV文件。,'w');
foreach($列表达领域){

文件fputcsv美元(美元等);

}

tag:文件操作实例标准电脑软件PHPSPL

相关内容