PHP文件处理类 SplFileObject 和 SplFileInfo

来源:互联网 发布:什么刻录软件最好用 编辑:程序博客网 时间:2024/06/02 04:21

文件处理类:

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. SplFileInfo {  
  2. /* 方法 */  
  3. public __construct ( string $file_name )  
  4. public int getATime ( void )     //获取文件的最后访问时间  
  5. public string getBasename ([ string $suffix ] )  //获取文件的没有路径信息的基本名称,参数可以为文件后缀,若有参数则返回没有该后缀的文件基本名称。  
  6. public int getCTime ( void )        //返回文章最后一次变更的时间戳。  
  7. public string getExtension ( void )   //获取文件扩展名  
  8. public SplFileInfo getFileInfo ([ string $class_name ] )  //以对象的形式返回文件路径和名称  
  9. public string getFilename ( void )   //获取文件名称,不带路径  
  10. public int getGroup ( void )   //获取文件所在组,返回组id  
  11. public int getInode ( void )    //获取文件索引节点  
  12. public string getLinkTarget ( void )  //获取文件链接目标  
  13. public int getMTime ( void )  //获取最后修改时间  
  14. public int getOwner ( void )     //获取文件的所有者  
  15. public string getPath ( void )   //获取文件路径,不带文件名和最后的斜杠  
  16. public SplFileInfo getPathInfo ([ string $class_name ] )   //返回路径对象  
  17. public string getPathname ( void )   //获取文件路径  
  18. public int getPerms ( void )   //获取文件权限  
  19. public string getRealPath ( void )  //获取文件绝对路径,若文件不存在,返回false  
  20. public int getSize ( void )   //返回文件大小,单位字节  
  21. public string getType ( void )  //返回文件类型,可能是 file, link, dir  
  22. public bool isDir ( void )  //判断是否是目录,是放回true否则返回false  
  23. public bool isExecutable ( void )  //判断文件是否可执行,返回true,否则返回false  
  24. public bool isFile ( void )   //如果文件存在且是一个普通文件(不是链接),返回true,否则返回false  
  25. public bool isLink ( void )  //判断文件是否是连接,不是返回false  
  26. public bool isReadable ( void )   //判断文件是否可读,可读返回true  
  27. public bool isWritable ( void )    //判断文件是否可写,可写返回true  
  28. public SplFileObject openFile ([ string $open_mode = "r" [, bool $use_include_path = false [, resource $context = NULL ]]] ) //获取文件对象信息  
  29. public void setFileClass ([ string $class_name = "SplFileObject" ] )  
  30. public void setInfoClass ([ string $class_name = "SplFileInfo" ] )  
  31. public void __toString ( void )   //以字符串的形式返回文件路径及名称  
  32. }  

使用方法:

$info = new SplFileInfo($file_name);

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. SplFileObject {  
  2. /* 常量 */  
  3. const integer DROP_NEW_LINE = 1 ;  
  4. const integer READ_AHEAD = 2 ;  
  5. const integer SKIP_EMPTY = 4 ;  
  6. const integer READ_CSV = 8 ;  
  7. /* 方法 */  
  8. public string|array current ( void )     //返回文件当前行内容  
  9. public bool eof ( void )  //检测文件是否到末尾,如果到末尾返回true,否则返回false  
  10. public bool fflush ( void )   //将缓冲内容输出到文件,成功时返回 TRUE, 或者在失败时返回 FALSE。  
  11. public string fgetc ( void )   //按字符读取文件  
  12. public array fgetcsv ([ string $delimiter = "," [, string $enclosure = "\"" [, string $escape = "\\" ]]] )   //读取csv文件  
  13. public string fgets ( void )   //按行读取文件  
  14. public string fgetss ([ string $allowable_tags ] )  //按行读取文件,并去掉html标记  
  15. public bool flock ( int $operation [, int &$wouldblock ] )   //文件锁定或解锁,返回true或false  
  16. /*参数: 
  17. LOCK_SH 共享锁 (读). 
  18. LOCK_EX 独占锁 (写). 
  19. LOCK_UN 释放锁 (共享或独占). 
  20. LOCK_NB (附加锁定) 如果不希望flock()在锁定时堵塞,则应在上述锁定后加上改锁(windows上不支持) 
  21. flock(LOCK_EX+LOCK_NB);   // 独占锁定加上附加锁定    
  22. flock(LOCK_UN+LOCK_NB); */  
  23. public int fpassthru ( void )   //输出文件指针之后的所有数据和字符数  
  24. public int fputcsv (array $fields)  //将一维数组作为一行输入csv文件中,返回写入的字符串长度或false  
  25. public string fread ( int $length )  //从文件中读取指定的字节数,返回读取的字符串或false  
  26. public mixed fscanf ( string $format [, mixed &$... ] )  //从文件中读取一行并按照指定模式解析  
  27. /*例: 
  28. $file = new SplFileObject("misc.txt"); 
  29. while ($userinfo = $file->fscanf("%s %s %s")) { 
  30.     list ($name, $profession, $countrycode) = $userinfo; 
  31.     // Do something with $name $profession $countrycode 
  32. }*/  
  33. public int fseek ( int $offset [, int $whence = SEEK_SET ] ) //按字节移动文件指针位置,  
  34. /*SEEK_SET 设置文件指针到指定字节位置(默认为该模式). 
  35. SEEK_CUR 设置文件指针到当前位置加上指定字节位置. 
  36. SEEK_END 设置文件指针到文件末尾加上指定字节位置(此时字节经常为负值). 
  37. public array fstat ( void )  //获取文件信息,以数组形式返回*/  
  38. /* 
  39. 数字下标    关联键名(自 PHP 4.0.6)   说明 
  40. 0           dev device number - 设备名 
  41. 1           ino inode number - inode 号码 
  42. 2           mode    inode protection mode - inode 保护模式 
  43. 3           nlink   number of links - 被连接数目 
  44. 4           uid userid of owner - 所有者的用户 id 
  45. 5           gid groupid of owner- 所有者的组 id 
  46. 6           rdev    device type, if inode device * - 设备类型,如果是 inode 设备的话 
  47. 7           size    size in bytes - 文件大小的字节数 
  48. 8           atime   time of last access (unix timestamp) - 上次访问时间(Unix 时间戳) 
  49. 9           mtime   time of last modification (unix timestamp) - 上次修改时间(Unix 时间戳) 
  50. 10          ctime   time of last change (unix timestamp) - 上次改变时间(Unix 时间戳) 
  51. 11          blksize blocksize of filesystem IO * - 文件系统 IO 的块大小 
  52. 12          blocks  number of blocks allocated - 所占据块的数目* Windows 下总是 0。 
  53. * - 仅在支持 st_blksize 类型的系统下有效。其它系统(如 Windows)返回 -1。 */  
  54.   
  55.   
  56. public int ftell ( void )  //返回当前文件位置,文件指针位置  
  57. public bool ftruncate ( int $size )   //将文件截断到指定的长度,若长度大于文件长度用空补齐(文件打开方法对其有影响)  
  58. public int fwrite ( string $str [, int $length ] )  //将$str字符串写入文件,只写$length长度。放回写入字节数或null  
  59. public array getCsvControl ( void )  //Gets the delimiter and enclosure character used for parsing CSV fields.  
  60. public int getFlags ( void )  //Gets the flags set for an instance of SplFileObject as an integer.  
  61. public int getMaxLineLen ( void )  //返回一行读取的最大字节数(在已设置的前提下),若未设置,默认为0  
  62. public int key ( void )  //获取当前行号。  
  63. public void next ( void )  //移动到下一行  
  64. public void rewind ( void ) //返回到第一行  
  65. public void seek ( int $line_pos )  //定位到文件指定行  
  66. public void setCsvControl ([ string $delimiter = "," [, string $enclosure = "\"" [, string $escape = "\\" ]]] )  
  67. public void setFlags ( int $flags )  
  68. public void setMaxLineLen ( int $max_len )  //设置文件读取一行的最大字节数,若文件每行有10个字符,但设置最大读取为  
  69. public bool valid ( void )  //检查是否到达文件底部,未到达底部返回 TRUE ,抵达返回false.  
  70. }  



用法:

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. $file = new SplFileObject("misc.txt"'r+');  
  2. while (!$file->eof()) {  
  3.     echo $file->current();  
  4.     $file->next();  
  5. }  
  6. //关闭文件对象  
  7. $file = null;  


常用文件处理方法:

[php] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <span style="font-size:18px;"><span style="font-size:18px;">/** 
  2.  * 获取文件指定行数范围数据 
  3.  * @param unknown $filename  文件名称 
  4.  * @param number $startLine  开始行 
  5.  * @param number $endLine    结束行 
  6.  * @param string $method 
  7.  * @return multitype: 
  8.  */  
  9. function getFileLines($filename$startLine = 1, $endLine = 20, $method = 'rb'){  
  10.     $content = array();  
  11.     $count = $endLine - $startLine;  
  12.     $fp = new SplFileObject($filename$method);  
  13.     $fp->seek($startLine - 1); // 转到第N行, seek方法参数从0开始计数  
  14.     for ($i = 0; $i <= $count; ++$i) {  
  15.         $content[] = $fp->current(); // current()获取当前行内容  
  16.         $fp->next(); // 下一行  
  17.         if($fp->eof()) {  
  18.             array_pop($content);  
  19.             break;  
  20.         }  
  21.     }  
  22.     return array_filter($content); // array_filter过滤:false,null,''  
  23. }  
  24.   
  25. /** 
  26.  * 获取文章最后一行内容 
  27.  * @param string $res 文件路径/名 
  28.  */  
  29. function get_last_line($res) {  
  30.     $fp = fopen($res'r');  
  31.     if (false == $fp) {  
  32.         return 'error';  
  33.     }  
  34.     fseek($fp,-1,SEEK_END);  
  35.     $s = '';  
  36.     while(($c = fgetc($fp)) !== false)  
  37.     {  
  38.         if($c == "\n" && $sbreak;  
  39.         $s = $c . $s;  
  40.         fseek($fp, -2, SEEK_CUR);  
  41.     }  
  42.     fclose($fp);  
  43.     return $s;  
  44. }</span></span>  


    转载自   http://blog.csdn.net/u013372487/article/details/51355174