SPL-StandardPHPLibrary
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes | List of all members
SplFileObject Class Reference
Inheritance diagram for SplFileObject:
Inheritance graph
Collaboration diagram for SplFileObject:
Collaboration graph

Public Member Functions

 __construct ($file_name, $open_mode= 'r', $use_include_path=false, $context=NULL)
 
 __toString ()
 
 current ()
 
 eof ()
 
 fflush ()
 
 fgetc ()
 
 fgetcsv ($delimiter=NULL, $enclosure=NULL)
 
 fgets ()
 
 fgetss ($allowable_tags=NULL)
 
 flock ($operation, &$wouldblock)
 
 fpassthru ()
 
 fscanf ($format)
 
 fseek ($pos, $whence=SEEK_SET)
 
 fstat ()
 
 ftell ()
 
 ftruncate ($size)
 
 fwrite ($str, $length=NULL)
 
 getATime ()
 
 getChildren ()
 
 getCsvControl ($delimiter= ',', $enclosure= '"')
 
 getCTime ()
 
 getCurrentLine ()
 
 getFileInfo (string class_name=NULL)
 
 getFilename ()
 
 getFlags ()
 
 getGroup ()
 
 getInode ()
 
 getLinkTarget ()
 
 getMaxLineLen ()
 
 getMTime ()
 
 getOwner ()
 
 getPath ()
 
 getPathInfo (string class_name=NULL)
 
 getPathname ()
 
 getPerms ()
 
 getRealPath ()
 
 getSize ()
 
 getType ()
 
 hasChildren ()
 
 isDir ()
 
 isExecutable ()
 
 isFile ()
 
 isLink ()
 
 isReadable ()
 
 isWritable ()
 
 key ()
 
 next ()
 
 openFile ($mode= 'r', $use_include_path=false, $context=NULL)
 
 rewind ()
 
 seek ($line_pos)
 
 setCsvControl ($delimiter= ';', $enclosure= '"')
 
 setFileClass (string class_name="SplFileObject")
 
 setFlags ($flags)
 
 setInfoClass (string class_name="SplFileInfo")
 
 setMaxLineLen ($max_len)
 
 valid ()
 

Public Attributes

const DROP_NEW_LINE = 0x00000001
 

Private Member Functions

 freeLine ()
 
 readLine ()
 

Private Attributes

 $delimiter = ','
 
 $enclosure = '"'
 
 $flags = 0
 
 $fname
 
 $fp
 
 $line = NULL
 
 $lnum = 0
 
 $max_len = 0
 

Detailed Description

Object representation for any stream.

Author
Marcus Boerger
Version
1.1
Since
PHP 5.1

Definition at line 18 of file splfileobject.inc.

Constructor & Destructor Documentation

SplFileObject::__construct (   $file_name,
  $open_mode = 'r',
  $use_include_path = false,
  $context = NULL 
)

Constructs a new file object.

Parameters
$file_nameThe name of the stream to open
$open_modeThe file open mode
$use_include_pathWhether to search in include paths
$contextA stream context
Exceptions
RuntimeExceptionIf file cannot be opened (e.g. insufficient access rights).

Definition at line 42 of file splfileobject.inc.

43  {
44  $this->fp = fopen($file_name, $open_mode, $use_include_path, $context);
45  if (!$this->fp)
46  {
47  throw new RuntimeException("Cannot open file $file_name");
48  }
49  $this->fname = $file_name;
50  }
Exception thrown for errors that are only detectable at runtime.
Definition: spl.php:423

Member Function Documentation

SplFileObject::__toString ( )
Returns
current line

Definition at line 359 of file splfileobject.inc.

References current().

360  {
361  return current();
362  }

Here is the call graph for this function:

SplFileObject::current ( )
Note
Fill current line buffer if not done yet.
Returns
line buffer

Implements Iterator.

Definition at line 284 of file splfileobject.inc.

References $line, and getCurrentLine().

Referenced by __toString().

285  {
286  if (is_null($this->line))
287  {
288  $this->line = getCurrentLine();
289  }
290  return $this->line;
291  }

Here is the call graph for this function:

SplFileObject::eof ( )
Returns
whether the end of the stream is reached

Definition at line 55 of file splfileobject.inc.

Referenced by getCurrentLine(), readLine(), seek(), and valid().

56  {
57  return eof($this->fp);
58  }
SplFileObject::fflush ( )

Flush current data.

Returns
success or failure

Definition at line 127 of file splfileobject.inc.

128  {
129  return fflush($this->fp);
130  }
fflush()
Flush current data.
SplFileObject::fgetc ( )
Returns
next char from file
Note
a new line character does not increase $this->lnum

Definition at line 155 of file splfileobject.inc.

References freeLine().

156  {
157  $this->freeLine();
158  $c = fgetc($this->fp);
159  if ($c == '\n') {
160  $this->lnum++;
161  }
162  }
freeLine()
Free the current line buffer and increment the line counter.

Here is the call graph for this function:

SplFileObject::fgetcsv (   $delimiter = NULL,
  $enclosure = NULL 
)
Parameters
delimitercharacter used as field separator
enclosureend of
Returns
array containing read data

Definition at line 77 of file splfileobject.inc.

References $delimiter, $enclosure, and freeLine().

78  {
79  $this->freeLine();
80  $this->lnum++;
81  switch(fun_num_args())
82  {
83  case 0:
85  case 1:
87  default:
88  case 2:
89  break;
90  }
91  return fgetcsv($this->fp, $this->max_len, $delimiter, $enclosure);
92  }
freeLine()
Free the current line buffer and increment the line counter.
fgetcsv($delimiter=NULL, $enclosure=NULL)

Here is the call graph for this function:

SplFileObject::fgets ( )

increase current line number

Returns
next line from stream

Definition at line 63 of file splfileobject.inc.

References freeLine().

Referenced by readLine().

64  {
65  $this->freeLine();
66  $this->lnum++;
67  $buf = fgets($this->fp, $this->max_len);
68 
69  return $buf;
70  }
freeLine()
Free the current line buffer and increment the line counter.
fgets()
increase current line number

Here is the call graph for this function:

SplFileObject::fgetss (   $allowable_tags = NULL)

Get a line from the file and strip HTML tags.

Parameters
$allowable_tagstags to keep in the string

Definition at line 175 of file splfileobject.inc.

176  {
177  return fgetss($this->fp, $allowable_tags);
178  }
fgetss($allowable_tags=NULL)
Get a line from the file and strip HTML tags.
SplFileObject::flock (   $operation,
$wouldblock 
)
Parameters
operationlock operation (LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB)
Return values
$wouldblockwhether the operation would block

Definition at line 118 of file splfileobject.inc.

119  {
120  return flock($this->fp, $operation, $wouldblock);
121  }
flock($operation, &$wouldblock)
SplFileObject::fpassthru ( )

Read and return remaining part of stream.

Returns
size of remaining part passed through

Definition at line 167 of file splfileobject.inc.

168  {
169  return fpassthru($this->fp);
170  }
fpassthru()
Read and return remaining part of stream.
SplFileObject::freeLine ( )
private

Free the current line buffer and increment the line counter.

Definition at line 334 of file splfileobject.inc.

Referenced by fgetc(), fgetcsv(), fgets(), fscanf(), getCurrentLine(), next(), readLine(), and rewind().

335  {
336  if ($this->line) {
337  $this->line = NULL;
338  }
339  }
SplFileObject::fscanf (   $format)

Scan the next line.

Parameters
$formatstring specifying format to parse

Definition at line 183 of file splfileobject.inc.

References freeLine().

184  {
185  $this->freeLine();
186  $this->lnum++;
187  return fscanf($this->fp, $format /* , ... */);
188  }
freeLine()
Free the current line buffer and increment the line counter.
fscanf($format)
Scan the next line.

Here is the call graph for this function:

SplFileObject::fseek (   $pos,
  $whence = SEEK_SET 
)
Parameters
posnew file position
whenceseek method (SEEK_SET, SEEK_CUR, SEEK_END)
Returns
Upon success, returns 0; otherwise, returns -1. Note that seeking past EOF is not considered an error.

Definition at line 146 of file splfileobject.inc.

147  {
148  return fseek($this->fp, $pos, $whence);
149  }
fseek($pos, $whence=SEEK_SET)
SplFileObject::fstat ( )
Returns
array of file stat information

Definition at line 202 of file splfileobject.inc.

203  {
204  return fstat($this->fp);
205  }
SplFileObject::ftell ( )
Returns
current file position

Definition at line 135 of file splfileobject.inc.

136  {
137  return ftell($this->fp);
138  }
SplFileObject::ftruncate (   $size)
Parameters
$sizenew size to truncate file to

Definition at line 210 of file splfileobject.inc.

211  {
212  return ftruncate($this->fp, $size);
213  }
SplFileObject::fwrite (   $str,
  $length = NULL 
)
Parameters
$strto write
$lengthmaximum line length to write

Definition at line 194 of file splfileobject.inc.

References $length.

195  {
196  return fwrite($this->fp, $length);
197  }
fwrite($str, $length=NULL)
$length
SplFileInfo::getATime ( )
inherited
Returns
The current entry's last access time.

Definition at line 910 of file spl.php.

910 {}
SplFileObject::getChildren ( )
Returns
false

Implements RecursiveIterator.

Definition at line 258 of file splfileobject.inc.

259  {
260  return NULL;
261  }
SplFileObject::getCsvControl (   $delimiter = ',',
  $enclosure = '"' 
)
Returns
array(delimiter, enclosure) as used in fgetcsv

Definition at line 109 of file splfileobject.inc.

110  {
111  return array($this->delimiter, $this->enclosure);
112  }
SplFileInfo::getCTime ( )
inherited
Returns
The current entry's last change time.

Definition at line 918 of file spl.php.

918 {}
SplFileObject::getCurrentLine ( )

Definition at line 346 of file splfileobject.inc.

References eof(), freeLine(), and readLine().

Referenced by current(), and seek().

347  {
348  $this->freeLine();
349  if ($this->eof())
350  {
351  throw new RuntimeException("Cannot read from file " . $this->fname);
352  }
353  $this->readLine();
354  }
freeLine()
Free the current line buffer and increment the line counter.
Exception thrown for errors that are only detectable at runtime.
Definition: spl.php:423

Here is the call graph for this function:

SplFileInfo::getFileInfo ( string  class_name = NULL)
inherited
Returns
SplFileInfo created for the file
Parameters
class_namename of class to instantiate
See Also
SplFileInfo::setInfoClass()

Definition at line 876 of file spl.php.

876 {}
SplFileInfo::getFilename ( )
inherited
Returns
the filename only.

Definition at line 870 of file spl.php.

870 {}
SplFileObject::getFlags ( )
Returns
current set of flags

Definition at line 226 of file splfileobject.inc.

References $flags.

227  {
228  return $this->flags;
229  }
SplFileInfo::getGroup ( )
inherited
Returns
The current entry's group name.

Definition at line 906 of file spl.php.

906 {}
SplFileInfo::getInode ( )
inherited
Returns
The current entry's inode.

Definition at line 894 of file spl.php.

894 {}
SplFileInfo::getLinkTarget ( )
inherited
Returns
target of link.

Definition at line 950 of file spl.php.

950 {}
SplFileObject::getMaxLineLen ( )
Returns
current setting for max line

Definition at line 242 of file splfileobject.inc.

References $max_len.

243  {
244  return $this->max_len;
245  }
SplFileInfo::getMTime ( )
inherited
Returns
The current entry's last modification time.

Definition at line 914 of file spl.php.

914 {}
SplFileInfo::getOwner ( )
inherited
Returns
The current entry's owner name.

Definition at line 902 of file spl.php.

902 {}
SplFileInfo::getPath ( )
inherited
Returns
the path part only.

Definition at line 866 of file spl.php.

866 {}
SplFileInfo::getPathInfo ( string  class_name = NULL)
inherited
Returns
SplFileInfo created for the path
Parameters
class_namename of class to instantiate
See Also
SplFileInfo::setInfoClass()

Definition at line 886 of file spl.php.

886 {}
SplFileInfo::getPathname ( )
inherited
Returns
The current entries path and file name.

Definition at line 880 of file spl.php.

880 {}
SplFileInfo::getPerms ( )
inherited
Returns
The current entry's permissions.

Definition at line 890 of file spl.php.

890 {}
SplFileInfo::getRealPath ( )
inherited
Returns
The resolved path

Definition at line 954 of file spl.php.

954 {}
SplFileInfo::getSize ( )
inherited
Returns
The current entry's size in bytes .

Definition at line 898 of file spl.php.

898 {}
SplFileInfo::getType ( )
inherited
Returns
The current entry's file type.

Definition at line 922 of file spl.php.

922 {}
SplFileObject::hasChildren ( )
Returns
false

Implements RecursiveIterator.

Definition at line 250 of file splfileobject.inc.

251  {
252  return false;
253  }
SplFileInfo::isDir ( )
inherited
Returns
Whether the current entry is a directory.

Definition at line 942 of file spl.php.

942 {}
SplFileInfo::isExecutable ( )
inherited
Returns
Whether the current entry is executable.

Definition at line 934 of file spl.php.

934 {}
SplFileInfo::isFile ( )
inherited
Returns
Whether the current entry is .

Definition at line 938 of file spl.php.

938 {}
SplFileInfo::isLink ( )
inherited
Returns
whether the current entry is a link.

Definition at line 946 of file spl.php.

946 {}
SplFileInfo::isReadable ( )
inherited
Returns
Whether the current entry is readable.

Definition at line 930 of file spl.php.

930 {}
SplFileInfo::isWritable ( )
inherited
Returns
Whether the current entry is writeable.

Definition at line 926 of file spl.php.

926 {}
SplFileObject::key ( )
Returns
line number
Note
fgetc() will increase the line number when reaing a new line char. This has the effect key() called on a read a new line will already return the increased line number.
Line counting works as long as you only read the file and do not use fseek().

Implements Iterator.

Definition at line 301 of file splfileobject.inc.

References $lnum.

302  {
303  return $this->lnum;
304  }
SplFileObject::next ( )

Invalidate current line buffer.

Implements Iterator.

Definition at line 308 of file splfileobject.inc.

References freeLine().

309  {
310  $this->freeLine();
311  }
freeLine()
Free the current line buffer and increment the line counter.

Here is the call graph for this function:

SplFileInfo::openFile (   $mode = 'r',
  $use_include_path = false,
  $context = NULL 
)
inherited

Open the current file as a SplFileObject instance.

Parameters
modeopen mode
use_include_pathwhether to search include paths (don't use)
contextresource context to pased to open function
Exceptions
RuntimeExceptionif file cannot be opened (e.g. insufficient access rights).
Returns
The opened file as a SplFileObject instance
See Also
SplFileObject
SplFileInfo::setFileClass()
file()

Definition at line 973 of file spl.php.

973 {}
SplFileObject::readLine ( )
private
Returns
next line read from file and increase the line counter

Definition at line 316 of file splfileobject.inc.

References $line, eof(), fgets(), and freeLine().

Referenced by getCurrentLine().

317  {
318  if ($this->eof())
319  {
320  $this->freeLine();
321  throw new RuntimeException("Cannot read from file " . $this->fname);
322  }
323  if ($this->line) {
324  $this->lnum++;
325  }
326  $this->freeLine();
327  $this->line = fgets($this->fp, $this->max_len);
328  return $this->line;
329  }
freeLine()
Free the current line buffer and increment the line counter.
fgets()
increase current line number
Exception thrown for errors that are only detectable at runtime.
Definition: spl.php:423

Here is the call graph for this function:

SplFileObject::rewind ( )

Invalidate current line buffer and set line number to 0.

Implements Iterator.

Definition at line 266 of file splfileobject.inc.

References freeLine().

Referenced by seek().

267  {
268  $this->freeLine();
269  $this->lnum = 0;
270  }
freeLine()
Free the current line buffer and increment the line counter.

Here is the call graph for this function:

SplFileObject::seek (   $line_pos)
Parameters
$line_posSeek to this line

Implements SeekableIterator.

Definition at line 367 of file splfileobject.inc.

References eof(), getCurrentLine(), and rewind().

368  {
369  $this->rewind();
370  while($this->lnum < $line_pos && !$this->eof())
371  {
372  $this->getCurrentLine();
373  }
374  }
rewind()
Invalidate current line buffer and set line number to 0.

Here is the call graph for this function:

SplFileObject::setCsvControl (   $delimiter = ';',
  $enclosure = '"' 
)

Set the delimiter and enclosure character used in fgetcsv.

Parameters
delimiternew delimiter, defaults to ','
enclosurenew enclosure, defaults to '"'

Definition at line 100 of file splfileobject.inc.

References $delimiter, and $enclosure.

101  {
102  $this->delimiter = $delimiter;
103  $this->enclosure = $enclosure;
104  }
SplFileInfo::setFileClass ( string  class_name = "SplFileObject")
inherited
Parameters
class_namename of class used with openFile(). Must be derived from SPLFileObject.

Definition at line 978 of file spl.php.

978 {}
SplFileObject::setFlags (   $flags)
Parameters
$flagsnew flag set

Definition at line 218 of file splfileobject.inc.

References $flags.

219  {
220  $this->flags = $flags;
221  }
SplFileInfo::setInfoClass ( string  class_name = "SplFileInfo")
inherited
Parameters
class_namename of class used with getFileInfo(), getPathInfo(). Must be derived from SplFileInfo.

Definition at line 983 of file spl.php.

983 {}
SplFileObject::setMaxLineLen (   $max_len)
Parameters
$max_lenset the maximum line length read

Definition at line 234 of file splfileobject.inc.

References $max_len.

235  {
236  $this->max_len = $max_len;
237  }
SplFileObject::valid ( )
Returns
whether more data can be read

Implements Iterator.

Definition at line 275 of file splfileobject.inc.

References eof().

276  {
277  return !$this->eof();
278  }

Here is the call graph for this function:

Member Data Documentation

SplFileObject::$delimiter = ','
private

Definition at line 29 of file splfileobject.inc.

Referenced by fgetcsv(), and setCsvControl().

SplFileObject::$enclosure = '"'
private

Definition at line 30 of file splfileobject.inc.

Referenced by fgetcsv(), and setCsvControl().

SplFileObject::$flags = 0
private

Definition at line 28 of file splfileobject.inc.

Referenced by getFlags(), and setFlags().

SplFileObject::$fname
private

Definition at line 24 of file splfileobject.inc.

SplFileObject::$fp
private

Definition at line 23 of file splfileobject.inc.

SplFileObject::$line = NULL
private

Definition at line 25 of file splfileobject.inc.

Referenced by current(), and readLine().

SplFileObject::$lnum = 0
private

Definition at line 26 of file splfileobject.inc.

Referenced by key().

SplFileObject::$max_len = 0
private

Definition at line 27 of file splfileobject.inc.

Referenced by getMaxLineLen(), and setMaxLineLen().

const SplFileObject::DROP_NEW_LINE = 0x00000001

Flag: wheter to suppress new lines.

Definition at line 21 of file splfileobject.inc.


The documentation for this class was generated from the following file: