PHP文件处理类 SplFileObject 和 SplFileInfo
来源:互联网 发布:什么刻录软件最好用 编辑:程序博客网 时间:2024/06/02 04:21
文件处理类:
- 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 )
- public bool fflush ( void )
- public string fgetc ( void )
- public array fgetcsv ([ string $delimiter = "," [, string $enclosure = "\"" [, string $escape = "\\" ]]] )
- public string fgets ( void )
- public string fgetss ([ string $allowable_tags ] )
- public bool flock ( int $operation [, int &$wouldblock ] )
-
-
-
-
-
-
-
- public int fpassthru ( void )
- public int fputcsv (array $fields)
- public string fread ( int $length )
- public mixed fscanf ( string $format [, mixed &$... ] )
-
-
-
-
-
-
- public int fseek ( int $offset [, int $whence = SEEK_SET ] )
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public int ftell ( void )
- public bool ftruncate ( int $size )
- public int fwrite ( string $str [, int $length ] )
- public array getCsvControl ( void )
- public int getFlags ( void )
- public int getMaxLineLen ( 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 bool valid ( void )
- }
用法:
- $file = new SplFileObject("misc.txt", 'r+');
- while (!$file->eof()) {
- echo $file->current();
- $file->next();
- }
-
- $file = null;
常用文件处理方法:
- <span style="font-size:18px;"><span style="font-size:18px;">
-
-
-
-
-
-
-
- function getFileLines($filename, $startLine = 1, $endLine = 20, $method = 'rb'){
- $content = array();
- $count = $endLine - $startLine;
- $fp = new SplFileObject($filename, $method);
- $fp->seek($startLine - 1);
- for ($i = 0; $i <= $count; ++$i) {
- $content[] = $fp->current();
- $fp->next();
- if($fp->eof()) {
- array_pop($content);
- break;
- }
- }
- return array_filter($content);
- }
-
-
-
-
-
- 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;
- }</span></span>
转载自 http://blog.csdn.net/u013372487/article/details/51355174