PHP SPL標(biāo)準(zhǔn)庫之文件操作(SplFileInfo和SplFileObject)實(shí)例
PHP SPL中提供了SplFileInfo和SplFileObject兩個(gè)類來處理文件操作。
SplFileInfo用來獲取文件詳細(xì)信息:
$file = new SplFileInfo('foo-bar.txt');
print_r(array(
'getATime' => $file->getATime(), //最后訪問時(shí)間
'getBasename' => $file->getBasename(), //獲取無路徑的basename
'getCTime' => $file->getCTime(), //獲取inode修改時(shí)間
'getExtension' => $file->getExtension(), //文件擴(kuò)展名
'getFilename' => $file->getFilename(), //獲取文件名
'getGroup' => $file->getGroup(), //獲取文件組
'getInode' => $file->getInode(), //獲取文件inode
'getLinkTarget' => $file->getLinkTarget(), //獲取文件鏈接目標(biāo)文件
'getMTime' => $file->getMTime(), //獲取最后修改時(shí)間
'getOwner' => $file->getOwner(), //文件擁有者
'getPath' => $file->getPath(), //不帶文件名的文件路徑
'getPathInfo' => $file->getPathInfo(), //上級(jí)路徑的SplFileInfo對(duì)象
'getPathname' => $file->getPathname(), //全路徑
'getPerms' => $file->getPerms(), //文件權(quán)限
'getRealPath' => $file->getRealPath(), //文件絕對(duì)路徑
'getSize' => $file->getSize(),//文件大小,單位字節(jié)
'getType' => $file->getType(),//文件類型 file dir link
'isDir' => $file->isDir(), //是否是目錄
'isFile' => $file->isFile(), //是否是文件
'isLink' => $file->isLink(), //是否是快捷鏈接
'isExecutable' => $file->isExecutable(), //是否可執(zhí)行
'isReadable' => $file->isReadable(), //是否可讀
'isWritable' => $file->isWritable(), //是否可寫
));
SplFileObject繼承SplFileInfo并實(shí)現(xiàn)RecursiveIterator , SeekableIterator接口 ,用于對(duì)文件遍歷、查找、操作
遍歷:
try {
foreach(new SplFileObject('foo-bar.txt') as $line) {
echo $line;
}
} catch (Exception $e) {
echo $e->getMessage();
}
查找指定行:
try {
$file = new SplFileObject('foo-bar.txt');
$file->seek(2);
echo $file->current();
} catch (Exception $e) {
echo $e->getMessage();
}
寫入csv文件:
$list = array (
array( 'aaa' , 'bbb' , 'ccc' , 'dddd' ),
array( '123' , '456' , '7891' ),
array( '"aaa"' , '"bbb"' )
);
$file = new SplFileObject ( 'file.csv' , 'w' );
foreach ( $list as $fields ) {
$file -> fputcsv ( $fields );
}
- PHP SPL標(biāo)準(zhǔn)庫之?dāng)?shù)據(jù)結(jié)構(gòu)棧(SplStack)介紹
- PHP SPL標(biāo)準(zhǔn)庫之?dāng)?shù)據(jù)結(jié)構(gòu)堆(SplHeap)簡(jiǎn)單使用實(shí)例
- 解析PHP SPL標(biāo)準(zhǔn)庫的用法(遍歷目錄,查找固定條件的文件)
- PHP SPL標(biāo)準(zhǔn)庫之SplFixedArray使用實(shí)例
- PHP標(biāo)準(zhǔn)庫(PHP SPL)詳解
- PHP SPL標(biāo)準(zhǔn)庫中的常用函數(shù)介紹
- PHP SPL標(biāo)準(zhǔn)庫之接口(Interface)詳解
- PHP使用標(biāo)準(zhǔn)庫spl實(shí)現(xiàn)的觀察者模式示例
- PHP標(biāo)準(zhǔn)庫 (SPL)——Countable用法示例
相關(guān)文章
ThinkPHP 3.2 版本升級(jí)了哪些內(nèi)容
ThinkPHP 3.2發(fā)布了挺長(zhǎng)時(shí)間了,這里也總結(jié)下這次ThinkPHP 3.2到底發(fā)生了哪些變化,方便程序員們進(jìn)行開發(fā)。2015-03-03
PHP Curl出現(xiàn)403錯(cuò)誤的解決辦法
Laravel如何使用數(shù)據(jù)庫事務(wù)及捕獲事務(wù)失敗后的異常詳解
codeigniter教程之上傳視頻并使用ffmpeg轉(zhuǎn)flv示例
實(shí)例講解PHP設(shè)計(jì)模式編程中的簡(jiǎn)單工廠模式
Vagrant(WSL)+PHPStorm+Xdebu 斷點(diǎn)調(diào)試環(huán)境搭建

