您的位置 首页 PHP

PHP编程文件处理类SplFileObject和SplFileInfo用法实例分析

本文实例讲述了PHP编程文件处理类SplFileObject和SplFileInfo用法。分享给大家供大家参考,具体如下:

php对于大文件的处理除了用以下方法外还可以直接调用linux命令

文件处理类:

使用方法:

fscanf(“%s %s %s”)) { list ($name,$profession,$countrycode) = $userinfo; // Do something with $name $profession $countrycode }*/ public int fseek ( int $offset [,int $whence = SEEK_SET ] ) //按字节移动文件指针位置, /*SEEK_SET 设置文件指针到指定字节位置(默认为该模式). SEEK_CUR 设置文件指针到当前位置加上指定字节位置. SEEK_END 设置文件指针到文件末尾加上指定字节位置(此时字节经常为负值). 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 ] ) //将$str字符串写入文件,只写$length长度。放回写入字节数或null public array getCsvControl ( void ) //Gets the delimiter and enclosure character used for parsing CSV fields. public int getFlags ( void ) //Gets the flags set for an instance of SplFileObject as an integer. public int getMaxLineLen ( void ) //返回一行读取的最大字节数(在已设置的前提下),若未设置,默认为0 public int key ( void ) //获取当前行号。 public void next ( void ) //移动到下一行 public void rewind ( void ) //返回到第一行 public void seek ( int $line_pos ) //定位到文件指定行 public void setCsvControl ([ string $delimiter = “,string $escape = “\\” ]]] ) public void setFlags ( int $flags ) public void setMaxLineLen ( int $max_len ) //设置文件读取一行的最大字节数,若文件每行有10个字符,但设置最大读取为 public bool valid ( void ) //检查是否到达文件底部,未到达底部返回 TRUE ,抵达返回false. }

用法:

eof()) { echo $file->current(); $file->next(); } //关闭文件对象 $file = null;

常用文件处理方法:

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

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》及《》

希望本文所述对大家PHP程序设计有所帮助。

关于作者: dawei

【声明】:金华站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

热门文章