php splfileobject 文件末尾,PHP文件处理类 SplFileObject 和 SplFileInfo

文件处理类:

SplFileInfo {

/* 方法 */

public __construct ( string $file_name )

public int getATime ( void )     //获取文件的最后访问时间

public string getBasename ([ string $suffix ] )  //获取文件的没有路径信息的基本名称,参数可以为文件后缀,若有参数则返回没有该后缀的文件基本名称。

public int getCTime ( void )        //返回文章最后一次变更的时间戳。

public string getExtension ( void )   //获取文件扩展名

public SplFileInfo getFileInfo ([ string $class_name ] )

public string getFilename ( void )

public int getGroup ( void )

public int getInode ( void )

public string getLinkTarget ( void )

public int getMTime ( void )

public int getOwner ( void )     //获取文件的所有者

public string getPath ( void )

public SplFileInfo getPathInfo ([ string $class_name ] )

public string getPathname ( void )

public int getPerms ( void )

public string getRealPath ( void )

public int getSize ( void )

public string getType ( void )

public bool isDir ( void )

public bool isExecutable ( void )

public bool isFile ( void )

public bool isLink ( void )

public bool isReadable ( void )

public bool isWritable ( void )

public SplFileObject openFile ([ string $open_mode = "r" [, bool $use_include_path = false [, resource $context = NULL ]]] )

public void setFileClass ([ string $class_name = "SplFileObject" ] )

public void setInfoClass ([ string $class_name = "SplFileInfo" ] )

public void __toString ( void )

}

使用方法:

$info = new SplFileInfo($file_name);

SplFileObject {

/* 常量 */

const integer DROP_NEW_LINE = 1 ;

const integer READ_AHEAD = 2 ;

const integer SKIP_EMPTY = 4 ;

const integer READ_CSV = 8 ;

/* 方法 */

public string|array current ( void )     //返回文件当前行内容

public bool eof ( void )  //检测文件是否到末尾,如果到末尾返回true,否则返回false

public bool fflush ( void )   //将缓冲内容输出到文件,成功时返回 TRUE, 或者在失败时返回 FALSE。

public string fgetc ( void )   //按字符读取文件

public array fgetcsv ([ string $delimiter = "," [, string $enclosure = "\"" [, string $escape = "\\" ]]] )   //读取csv文件

public string fgets ( void )   //按行读取文件

public string fgetss ([ string $allowable_tags ] )  //按行读取文件,并去掉html标记

public bool flock ( int $operation [, int &$wouldblock ] )   //文件锁定或解锁,返回true或false

参数:

LOCK_SH 共享锁 (读).

LOCK_EX 独占锁 (写).

LOCK_UN 释放锁 (共享或独占).

LOCK_NB (附加锁定) 如果不希望flock()在锁定时堵塞,则应在上述锁定后加上改锁(windows上不支持)flock(LOCK_EX+LOCK_NB);   // 独占锁定加上附加锁定flock(LOCK_UN+LOCK_NB);

public int fpassthru ( void )   //输出文件指针之后的所有数据和字符数

public int fputcsv ( array $fields [, string $delimiter = "," [, string $enclosure = '"' [, string $escape = "\" ]]] )  //将一维数组作为一行输入csv文件中,返回写入的字符串长度或false

public string fread ( int $length )  //从文件中读取指定的字节数,返回读取的字符串或false

public mixed fscanf ( string $format [, mixed &$... ] )

public int fseek ( int $offset [, int $whence = SEEK_SET ] )

public array fstat ( void )  //获取文件信息,以数组形式返回

数字下标

关联键名(自 PHP 4.0.6)

说明

0

dev

device number - 设备名

1

ino

inode number - inode 号码

2

mode

inode protection mode - inode 保护模式

3

nlink

number of links - 被连接数目

4

uid

userid of owner - 所有者的用户 id

5

gid

groupid of owner- 所有者的组 id

6

rdev

device type, if inode device * - 设备类型,如果是 inode 设备的话

7

size

size in bytes - 文件大小的字节数

8

atime

time of last access (unix timestamp) - 上次访问时间(Unix 时间戳)

9

mtime

time of last modification (unix timestamp) - 上次修改时间(Unix 时间戳)

10

ctime

time of last change (unix timestamp) - 上次改变时间(Unix 时间戳)

11

blksize

blocksize of filesystem IO * - 文件系统 IO 的块大小

12

blocks

number of blocks allocated - 所占据块的数目

* Windows 下总是 0。

* - 仅在支持 st_blksize 类型的系统下有效。其它系统(如 Windows)返回 -1。

public int ftell ( void )  //返回当前文件位置,文件指针位置

public bool ftruncate ( int $size )

public int fwrite ( string $str [, int $length ] )

public void getChildren ( void )

public array getCsvControl ( void )

public int getFlags ( void )

public int getMaxLineLen ( void )

public bool hasChildren ( void )

public int key ( void )

public void next ( void )

public void rewind ( void )

public void seek ( int $line_pos )

public void setCsvControl ([ string $delimiter = "," [, string $enclosure = "\"" [, string $escape = "\\" ]]] )

public void setFlags ( int $flags )

public void setMaxLineLen ( int $max_len )

public void __toString ( void )

public bool valid ( void )

}

用法:

$file = new SplFileObject("misc.txt", 'r+');

while (!$file->eof()) {

echo $file->current();

$file->next();

}

常用文件处理方法:

/**

* 获取文件指定行数范围数据

* @param unknown $filename 文件名称

* @param number $startLine 开始行

* @param number $endLine 结束行

* @param string $method

* @return multitype:

*/

function getFileLines($filename, $startLine = 1, $endLine = 20, $method = 'rb'){

$content = array();

$count = $endLine - $startLine;

$fp = new SplFileObject($filename, $method);

$fp->seek($startLine - 1); // 转到第N行, seek方法参数从0开始计数

for ($i = 0; $i <= $count; ++$i) {

$content[] = $fp->current(); // current()获取当前行内容

$fp->next(); // 下一行

if($fp->eof()) {

array_pop($content);

break;

}

}

return array_filter($content); // array_filter过滤:false,null,''

}

/**

* 获取文章最后一行内容

* @param string $res 文件路径/名

*/

function get_last_line($res) {

$fp = fopen($res, 'r');

if (false == $fp) {

return 'error';

}

fseek($fp,-1,SEEK_END);

$s = '';

while(($c = fgetc($fp)) !== false)

{

if($c == "\n" && $s) break;

$s = $c . $s;

fseek($fp, -2, SEEK_CUR);

}

fclose($fp);

return $s;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值