4 * Represents a file in the filesystem
6 * @warning Be sure to distinguish between get() and write() versus
7 * read() and put(), the former operates on the entire file, while
8 * the latter operates on a handle.
13 /** Filename of file this object represents */
16 /** Handle for the file */
17 protected $handle = false;
19 /** Instance of FSTools for interfacing with filesystem */
23 * Filename of file you wish to instantiate.
24 * @note This file need not exist
26 public function __construct($name, $fs = false)
29 $this->fs
= $fs ?
$fs : FSTools
::singleton();
32 /** Returns the filename of the file. */
33 public function getName() {return $this->name
;}
35 /** Returns directory of the file without trailing slash */
36 public function getDirectory() {return $this->fs
->dirname($this->name
);}
39 * Retrieves the contents of a file
40 * @todo Throw an exception if file doesn't exist
44 return $this->fs
->file_get_contents($this->name
);
47 /** Writes contents to a file, creates new file if necessary */
48 public function write($contents)
50 return $this->fs
->file_put_contents($this->name
, $contents);
53 /** Deletes the file */
54 public function delete()
56 return $this->fs
->unlink($this->name
);
59 /** Returns true if file exists and is a file. */
60 public function exists()
62 return $this->fs
->is_file($this->name
);
65 /** Returns last file modification time */
66 public function getMTime()
68 return $this->fs
->filemtime($this->name
);
73 * @note We ignore errors because of some weird owner trickery due
76 public function chmod($octal_code)
78 return @$this->fs
->chmod($this->name
, $octal_code);
81 /** Opens file's handle */
82 public function open($mode)
84 if ($this->handle
) $this->close();
85 $this->handle
= $this->fs
->fopen($this->name
, $mode);
89 /** Closes file's handle */
90 public function close()
92 if (!$this->handle
) return false;
93 $status = $this->fs
->fclose($this->handle
);
94 $this->handle
= false;
98 /** Retrieves a line from an open file, with optional max length $length */
99 public function getLine($length = null)
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);
106 /** Retrieves a character from an open file */
107 public function getChar()
109 if (!$this->handle
) $this->open('r');
110 return $this->fs
->fgetc($this->handle
);
113 /** Retrieves an $length bytes of data from an open data */
114 public function read($length)
116 if (!$this->handle
) $this->open('r');
117 return $this->fs
->fread($this->handle
, $length);
120 /** Writes to an open file */
121 public function put($string)
123 if (!$this->handle
) $this->open('a');
124 return $this->fs
->fwrite($this->handle
, $string);
127 /** Returns TRUE if the end of the file has been reached */
128 public function eof()
130 if (!$this->handle
) return true;
131 return $this->fs
->feof($this->handle
);
134 public function __destruct()
136 if ($this->handle
) $this->close();
141 // vim: et sw=4 sts=4