PHP SPL中提供了SplFileInfo和SplFileObject两个类来处理文件操作。
SplFileInfo用来获取文件详细信息:
RecursiveIterator , SeekableIterator接口 ,用于对文件遍历、查找、操作
遍历:
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 );
}
本文地址:https://www.stayed.cn/item/23520
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我