ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
FSTools_File Class Reference

Represents a file in the filesystem. More...

+ Collaboration diagram for FSTools_File:

Public Member Functions

 __construct ($name, $fs=false)
 Filename of file you wish to instantiate. More...
 
 getName ()
 Returns the filename of the file. More...
 
 getDirectory ()
 Returns directory of the file without trailing slash. More...
 
 get ()
 Retrieves the contents of a file. More...
 
 write ($contents)
 Writes contents to a file, creates new file if necessary. More...
 
 delete ()
 Deletes the file. More...
 
 exists ()
 Returns true if file exists and is a file. More...
 
 getMTime ()
 Returns last file modification time. More...
 
 chmod ($octal_code)
 Chmod a file. More...
 
 open ($mode)
 Opens file's handle. More...
 
 close ()
 Closes file's handle. More...
 
 getLine ($length=null)
 Retrieves a line from an open file, with optional max length $length. More...
 
 getChar ()
 Retrieves a character from an open file. More...
 
 read ($length)
 Retrieves an $length bytes of data from an open data. More...
 
 put ($string)
 Writes to an open file. More...
 
 eof ()
 Returns TRUE if the end of the file has been reached. More...
 
 __destruct ()
 

Protected Attributes

 $name
 Filename of file this object represents. More...
 
 $handle = false
 Handle for the file. More...
 
 $fs
 Instance of FSTools for interfacing with filesystem. More...
 

Detailed Description

Represents a file in the filesystem.

Warning
Be sure to distinguish between get() and write() versus read() and put(), the former operates on the entire file, while the latter operates on a handle.

Definition at line 10 of file File.php.

Constructor & Destructor Documentation

◆ __construct()

FSTools_File::__construct (   $name,
  $fs = false 
)

Filename of file you wish to instantiate.

Note
This file need not exist

Definition at line 26 of file File.php.

References $fs, $name, and FSTools\singleton().

27  {
28  $this->name = $name;
29  $this->fs = $fs ? $fs : FSTools::singleton();
30  }
$name
Filename of file this object represents.
Definition: File.php:14
$fs
Instance of FSTools for interfacing with filesystem.
Definition: File.php:20
static singleton()
Returns a global instance of FSTools.
Definition: FSTools.php:18
+ Here is the call graph for this function:

◆ __destruct()

FSTools_File::__destruct ( )

Definition at line 134 of file File.php.

References close().

135  {
136  if ($this->handle) $this->close();
137  }
close()
Closes file's handle.
Definition: File.php:90
+ Here is the call graph for this function:

Member Function Documentation

◆ chmod()

FSTools_File::chmod (   $octal_code)

Chmod a file.

Note
We ignore errors because of some weird owner trickery due to SVN duality

Definition at line 76 of file File.php.

77  {
78  return @$this->fs->chmod($this->name, $octal_code);
79  }

◆ close()

FSTools_File::close ( )

Closes file's handle.

Definition at line 90 of file File.php.

Referenced by __destruct(), and open().

91  {
92  if (!$this->handle) return false;
93  $status = $this->fs->fclose($this->handle);
94  $this->handle = false;
95  return $status;
96  }
+ Here is the caller graph for this function:

◆ delete()

FSTools_File::delete ( )

Deletes the file.

Definition at line 54 of file File.php.

55  {
56  return $this->fs->unlink($this->name);
57  }

◆ eof()

FSTools_File::eof ( )

Returns TRUE if the end of the file has been reached.

Definition at line 128 of file File.php.

129  {
130  if (!$this->handle) return true;
131  return $this->fs->feof($this->handle);
132  }

◆ exists()

FSTools_File::exists ( )

Returns true if file exists and is a file.

Definition at line 60 of file File.php.

61  {
62  return $this->fs->is_file($this->name);
63  }

◆ get()

FSTools_File::get ( )

Retrieves the contents of a file.

Todo:
Throw an exception if file doesn't exist

Definition at line 42 of file File.php.

43  {
44  return $this->fs->file_get_contents($this->name);
45  }

◆ getChar()

FSTools_File::getChar ( )

Retrieves a character from an open file.

Definition at line 107 of file File.php.

References open().

108  {
109  if (!$this->handle) $this->open('r');
110  return $this->fs->fgetc($this->handle);
111  }
open($mode)
Opens file's handle.
Definition: File.php:82
+ Here is the call graph for this function:

◆ getDirectory()

FSTools_File::getDirectory ( )

Returns directory of the file without trailing slash.

Definition at line 36 of file File.php.

36 {return $this->fs->dirname($this->name);}

◆ getLine()

FSTools_File::getLine (   $length = null)

Retrieves a line from an open file, with optional max length $length.

Definition at line 99 of file File.php.

References open().

100  {
101  if (!$this->handle) $this->open('r');
102  if ($length === null) return $this->fs->fgets($this->handle);
103  else return $this->fs->fgets($this->handle, $length);
104  }
open($mode)
Opens file's handle.
Definition: File.php:82
+ Here is the call graph for this function:

◆ getMTime()

FSTools_File::getMTime ( )

Returns last file modification time.

Definition at line 66 of file File.php.

67  {
68  return $this->fs->filemtime($this->name);
69  }

◆ getName()

FSTools_File::getName ( )

Returns the filename of the file.

Definition at line 33 of file File.php.

References $name.

33 {return $this->name;}
$name
Filename of file this object represents.
Definition: File.php:14

◆ open()

FSTools_File::open (   $mode)

Opens file's handle.

Definition at line 82 of file File.php.

References close().

Referenced by getChar(), getLine(), put(), and read().

83  {
84  if ($this->handle) $this->close();
85  $this->handle = $this->fs->fopen($this->name, $mode);
86  return true;
87  }
close()
Closes file's handle.
Definition: File.php:90
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ put()

FSTools_File::put (   $string)

Writes to an open file.

Definition at line 121 of file File.php.

References open().

122  {
123  if (!$this->handle) $this->open('a');
124  return $this->fs->fwrite($this->handle, $string);
125  }
open($mode)
Opens file's handle.
Definition: File.php:82
+ Here is the call graph for this function:

◆ read()

FSTools_File::read (   $length)

Retrieves an $length bytes of data from an open data.

Definition at line 114 of file File.php.

References open().

115  {
116  if (!$this->handle) $this->open('r');
117  return $this->fs->fread($this->handle, $length);
118  }
open($mode)
Opens file's handle.
Definition: File.php:82
+ Here is the call graph for this function:

◆ write()

FSTools_File::write (   $contents)

Writes contents to a file, creates new file if necessary.

Definition at line 48 of file File.php.

References $contents.

49  {
50  return $this->fs->file_put_contents($this->name, $contents);
51  }

Field Documentation

◆ $fs

FSTools_File::$fs
protected

Instance of FSTools for interfacing with filesystem.

Definition at line 20 of file File.php.

Referenced by __construct().

◆ $handle

FSTools_File::$handle = false
protected

Handle for the file.

Definition at line 17 of file File.php.

◆ $name

FSTools_File::$name
protected

Filename of file this object represents.

Definition at line 14 of file File.php.

Referenced by __construct(), and getName().


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