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


Posted in PHP onJuly 22, 2017

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

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

文件处理类:

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 )  //获取文件所在组,返回组id
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 ) //获取文件绝对路径,若文件不存在,返回false
public int getSize ( void )  //返回文件大小,单位字节
public string getType ( void ) //返回文件类型,可能是 file, link, dir
public bool isDir ( void ) //判断是否是目录,是放回true否则返回false
public bool isExecutable ( void ) //判断文件是否可执行,返回true,否则返回false
public bool isFile ( void )  //如果文件存在且是一个普通文件(不是链接),返回true,否则返回false
public bool isLink ( void ) //判断文件是否是连接,不是返回false
public bool isReadable ( void )  //判断文件是否可读,可读返回true
public bool isWritable ( void )  //判断文件是否可写,可写返回true
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) //将一维数组作为一行输入csv文件中,返回写入的字符串长度或false
public string fread ( int $length ) //从文件中读取指定的字节数,返回读取的字符串或false
public mixed fscanf ( string $format [, mixed &$... ] ) //从文件中读取一行并按照指定模式解析
/*例:
$file = new SplFileObject("misc.txt");
while ($userinfo = $file->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 $enclosure = "\"" [, string $escape = "\\" ]]] )
public void setFlags ( int $flags )
public void setMaxLineLen ( int $max_len ) //设置文件读取一行的最大字节数,若文件每行有10个字符,但设置最大读取为
public bool valid ( void ) //检查是否到达文件底部,未到达底部返回 TRUE ,抵达返回false.
}

用法:

$file = new SplFileObject("misc.txt", 'r+');
while (!$file->eof()) {
  echo $file->current();
  $file->next();
}
//关闭文件对象
$file = null;

常用文件处理方法:

/**
 * 获取文件指定行数范围数据
 * @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;
}

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

PHP 相关文章推荐
PHP树的代码,可以嵌套任意层
Oct 09 PHP
Win2000+Apache+MySql+PHP4+PERL安装使用小结
Oct 09 PHP
jq的get传参数在utf-8中乱码问题的解决php版
Jul 23 PHP
用mysql触发器自动更新memcache的实现代码
Oct 11 PHP
CI框架验证码CAPTCHA辅助函数用法实例
Nov 05 PHP
php使用cookie显示用户上次访问网站日期的方法
Jan 26 PHP
php简单生成随机数的方法
Jul 30 PHP
php cli配置文件问题分析
Oct 15 PHP
PHP实现对二维数组某个键排序的方法
Sep 14 PHP
laravel框架 laravel-admin上传图片到oss的方法
Oct 13 PHP
php进程(线程)通信基础之System V共享内存简单实例分析
Nov 09 PHP
PHP基本语法
Mar 31 PHP
PHP编程快速实现数组去重的方法详解
Jul 22 #PHP
php使用ftp实现文件上传与下载功能
Jul 21 #PHP
YII2框架中excel表格导出的方法详解
Jul 21 #PHP
实例讲解YII2中多表关联的使用方法
Jul 21 #PHP
PHP实现表单提交数据的验证处理功能【防SQL注入和XSS攻击等】
Jul 21 #PHP
php实现基于pdo的事务处理方法示例
Jul 21 #PHP
php基于自定义函数记录log日志方法
Jul 21 #PHP
You might like
php下用GD生成生成缩略图的两个选择和区别
2007/04/17 PHP
PHP 字符串分割和比较
2009/10/06 PHP
PHP中函数rand和mt_rand的区别比较
2012/12/26 PHP
通过table标签,PHP输出EXCEL的实现方法
2013/07/24 PHP
php获取、检查类名、函数名、方法名的函数方法
2015/06/25 PHP
PHP explode()函数的几个应用和implode()函数有什么区别
2015/11/05 PHP
PHP实现支付宝即时到账功能
2016/12/21 PHP
php生成二维码不保存服务器还有下载功能的实现代码
2018/08/09 PHP
jQuery 获取对象 基本选择与层级
2010/05/31 Javascript
js实现当前输入框高亮显示的方法
2015/08/19 Javascript
jQuery实现Meizu魅族官方网站的导航菜单效果
2015/09/14 Javascript
Jquery插件仿百度搜索关键字自动匹配功能
2016/05/11 Javascript
Angular指令之restict匹配模式的详解
2017/07/27 Javascript
Vue 幸运大转盘实现思路详解
2019/05/06 Javascript
js表达式与运算符简单操作示例
2020/02/15 Javascript
[00:36]DOTA2风云人物相约完美“圣”典 12月17日不见不散
2016/11/30 DOTA
python strip()函数 介绍
2013/05/24 Python
python中global与nonlocal比较
2014/11/21 Python
python从入门到精通(DAY 2)
2015/12/20 Python
Python 中开发pattern的string模板(template) 实例详解
2017/04/01 Python
Python 网页解析HTMLParse的实例详解
2017/08/10 Python
对TensorFlow的assign赋值用法详解
2018/07/30 Python
python程序控制NAO机器人行走
2019/04/29 Python
Django连接数据库并实现读写分离过程解析
2019/11/13 Python
.dcm格式文件软件读取及python处理详解
2020/01/16 Python
对pytorch的函数中的group参数的作用介绍
2020/02/18 Python
将pymysql获取到的数据类型是tuple转化为pandas方式
2020/05/15 Python
Win 10下Anaconda虚拟环境的教程
2020/05/18 Python
Kmeans均值聚类算法原理以及Python如何实现
2020/09/26 Python
python中random模块详解
2021/03/01 Python
美国办公用品购物网站:Quill.com
2016/09/01 全球购物
MVMT手表官方网站:时尚又实惠的高品质手表
2016/12/04 全球购物
花店创业计划书范文
2014/02/07 职场文书
手术室护士长竞聘书
2014/03/31 职场文书
2014办公室副主任四风对照检查材料思想汇报
2014/09/20 职场文书
用Python爬取英雄联盟的皮肤详细示例
2021/12/06 Python