Copyright, Lee Babin (lee@thecodeshoppe.com)
This code may is used and redistributed without charge
Under the terms of the GNU general public
License version 2.0 or later--www.gnu.org
Subject to the retention of this copyright
and GPL Notice in all copies or derived works
The path to the file we wish to work with.
Error messages in the form of constants for ease.
Constfounderror = "Sorry, the file in question does not exist.";
Constpermerror = "Sorry, you don't have the proper permissions on this file";
Constopenerror = "Sorry, the file in question could is not opened.";
Constcloseerror = "Sorry, the file could not to be closed.";
The constructor function.
Publicfunction__construct () {
$num _args= Func_num_args ();
$args = Func_get_args ();
$this->thepath = $args [0];
A function to open the file.
Privatefunctionopenfile ($readorwrite) {
if (file_exists ($this->thepath)) {
Now, we need to the if we are reading or writing or both.
if ($readorwrite = = "R") {
if (is_readable ($this->thepath)) {
}elseif ($readorwrite = = "W") {
if (is_writable ($this->thepath)) {
if (is_readable ($this->thepath) &&is_writable ($this->thepath)) {
We can now attempt to open the file.
if ($filepointer =fopen ($this->thepath, $readorwrite)) {
Thrownewexception (Self::openerror);
Thrownewexception (self::P ermerror);
Thrownewexception (Self::founderror);
A function to close a file.
if (!fclose ($this->thepath)) {
Thrownewexception (Self::closeerror);
A function to read a file, then return the results of the "read in a string."
The attempt to open the file.
$filepointer = $this->openfile ("R");
Now, return a string with the read data.
if ($filepointer!= false) {
Then we can read the file.
Returnfgets ($filepointer);
A function to write to a file.
Publicfunctionwrite ($towrite) {
The attempt to open the file.
$filepointer = $this->openfile ("w");
Now, return a string with the read data.
if ($filepointer!= false) {
Then we can read the file.
Returnfwrite ($filepointer, $towrite);
A function to append to a file.
Publicfunctionappend ($toappend) {
The attempt to open the file.
$filepointer = $this->openfile ("a");
Now, return a string with the read data.
if ($filepointer!= false) {
Then we can read the file.
Returnfwrite ($filepointer, $toappend);
A function to set the path to a new file.
Publicfunctionsetpath ($newpath) {
$this->thepath = $newpath;