欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP SPL標(biāo)準(zhǔn)庫(kù)之文件操作(SplFileInfo和SplFileObject)實(shí)例

 更新時(shí)間:2015年05月11日 11:06:54   投稿:junjie  
這篇文章主要介紹了PHP SPL標(biāo)準(zhǔn)庫(kù)之文件操作(SplFileInfo和SplFileObject)實(shí)例,本文講解SplFileInfo用來(lái)獲取文件詳細(xì)信息、SplFileObject遍歷、查找指定行、寫入csv文件等內(nèi)容,需要的朋友可以參考下

PHP SPL中提供了SplFileInfo和SplFileObject兩個(gè)類來(lái)處理文件操作。

SplFileInfo用來(lái)獲取文件詳細(xì)信息:

復(fù)制代碼 代碼如下:

$file = new SplFileInfo('foo-bar.txt');
 
print_r(array(
    'getATime' => $file->getATime(), //最后訪問(wèn)時(shí)間
    'getBasename' => $file->getBasename(), //獲取無(wú)路徑的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ì)文件遍歷、查找、操作

遍歷:

復(fù)制代碼 代碼如下:

try {
    foreach(new SplFileObject('foo-bar.txt') as $line) {
        echo $line;
    }
} catch (Exception $e) {
    echo $e->getMessage();
}

查找指定行:
復(fù)制代碼 代碼如下:

try {
    $file = new SplFileObject('foo-bar.txt');
    $file->seek(2);
    echo $file->current();
} catch (Exception $e) {
    echo $e->getMessage();
}

寫入csv文件:
復(fù)制代碼 代碼如下:

$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 );
}

相關(guān)文章

最新評(píng)論