search
HomeBackend DevelopmentPHP TutorialUsage case analysis of PHP file processing classes SplFileObject and SplFileInfo

这篇文章主要介绍了PHP编程文件处理类SplFileObject和SplFileInfo用法,结合实例形式分析了文件处理类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,&#39;&#39;
}
/**
 * 获取文章最后一行内容
 * @param string $res 文件路径/名
 */
function get_last_line($res) {
  $fp = fopen($res, &#39;r&#39;);
  if (false == $fp) {
    return &#39;error&#39;;
  }
  fseek($fp,-1,SEEK_END);
  $s = &#39;&#39;;
  while(($c = fgetc($fp)) !== false)
  {
    if($c == "\n" && $s) break;
    $s = $c . $s;
    fseek($fp, -2, SEEK_CUR);
  }
  fclose($fp);
  return $s;
}

相关推荐:

php文件处理类中关于SplFileObject与SplFileInfo的具体详解

PHP文件处理的高级应用—文件指针

PHP文件处理的高级应用—远程文件访问及锁定文件

The above is the detailed content of Usage case analysis of PHP file processing classes SplFileObject and SplFileInfo. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.