2008-01-27 05:31:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a file in the filesystem
|
2008-01-27 05:53:13 +00:00
|
|
|
*
|
|
|
|
* @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.
|
2008-01-27 05:31:06 +00:00
|
|
|
*/
|
|
|
|
class FSTools_File
|
|
|
|
{
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Filename of file this object represents */
|
|
|
|
protected $name;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Handle for the file */
|
|
|
|
protected $handle = false;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Instance of FSTools for interfacing with filesystem */
|
|
|
|
protected $fs;
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/**
|
|
|
|
* Filename of file you wish to instantiate.
|
|
|
|
* @note This file need not exist
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function __construct($name, $fs = false)
|
|
|
|
{
|
2008-01-27 05:31:06 +00:00
|
|
|
$this->name = $name;
|
|
|
|
$this->fs = $fs ? $fs : FSTools::singleton();
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Returns the filename of the file. */
|
|
|
|
public function getName() {return $this->name;}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Returns directory of the file without trailing slash */
|
|
|
|
public function getDirectory() {return $this->fs->dirname($this->name);}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the contents of a file
|
|
|
|
* @todo Throw an exception if file doesn't exist
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function get()
|
|
|
|
{
|
2008-01-27 05:31:06 +00:00
|
|
|
return $this->fs->file_get_contents($this->name);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Writes contents to a file, creates new file if necessary */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function write($contents)
|
|
|
|
{
|
2008-01-27 05:31:06 +00:00
|
|
|
return $this->fs->file_put_contents($this->name, $contents);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Deletes the file */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function delete()
|
|
|
|
{
|
2008-01-27 05:31:06 +00:00
|
|
|
return $this->fs->unlink($this->name);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Returns true if file exists and is a file. */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function exists()
|
|
|
|
{
|
2008-01-27 05:31:06 +00:00
|
|
|
return $this->fs->is_file($this->name);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Returns last file modification time */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function getMTime()
|
|
|
|
{
|
2008-01-27 05:31:06 +00:00
|
|
|
return $this->fs->filemtime($this->name);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/**
|
|
|
|
* Chmod a file
|
|
|
|
* @note We ignore errors because of some weird owner trickery due
|
|
|
|
* to SVN duality
|
|
|
|
*/
|
2013-07-16 11:56:14 +00:00
|
|
|
public function chmod($octal_code)
|
|
|
|
{
|
2008-01-27 05:31:06 +00:00
|
|
|
return @$this->fs->chmod($this->name, $octal_code);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Opens file's handle */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function open($mode)
|
|
|
|
{
|
2008-01-27 05:31:06 +00:00
|
|
|
if ($this->handle) $this->close();
|
|
|
|
$this->handle = $this->fs->fopen($this->name, $mode);
|
|
|
|
return true;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
/** Closes file's handle */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function close()
|
|
|
|
{
|
2008-01-27 05:31:06 +00:00
|
|
|
if (!$this->handle) return false;
|
2008-01-27 05:53:13 +00:00
|
|
|
$status = $this->fs->fclose($this->handle);
|
2008-01-27 05:31:06 +00:00
|
|
|
$this->handle = false;
|
2008-01-27 05:53:13 +00:00
|
|
|
return $status;
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:53:13 +00:00
|
|
|
/** Retrieves a line from an open file, with optional max length $length */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function getLine($length = null)
|
|
|
|
{
|
2008-01-27 05:53:13 +00:00
|
|
|
if (!$this->handle) $this->open('r');
|
|
|
|
if ($length === null) return $this->fs->fgets($this->handle);
|
|
|
|
else return $this->fs->fgets($this->handle, $length);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:53:13 +00:00
|
|
|
/** Retrieves a character from an open file */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function getChar()
|
|
|
|
{
|
2008-01-27 05:53:13 +00:00
|
|
|
if (!$this->handle) $this->open('r');
|
|
|
|
return $this->fs->fgetc($this->handle);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:53:13 +00:00
|
|
|
/** Retrieves an $length bytes of data from an open data */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function read($length)
|
|
|
|
{
|
2008-01-27 05:53:13 +00:00
|
|
|
if (!$this->handle) $this->open('r');
|
|
|
|
return $this->fs->fread($this->handle, $length);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:53:13 +00:00
|
|
|
/** Writes to an open file */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function put($string)
|
|
|
|
{
|
2008-01-27 05:53:13 +00:00
|
|
|
if (!$this->handle) $this->open('a');
|
|
|
|
return $this->fs->fwrite($this->handle, $string);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:53:13 +00:00
|
|
|
/** Returns TRUE if the end of the file has been reached */
|
2013-07-16 11:56:14 +00:00
|
|
|
public function eof()
|
|
|
|
{
|
2008-01-27 05:53:13 +00:00
|
|
|
if (!$this->handle) return true;
|
|
|
|
return $this->fs->feof($this->handle);
|
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2013-07-16 11:56:14 +00:00
|
|
|
public function __destruct()
|
|
|
|
{
|
2008-01-27 05:53:13 +00:00
|
|
|
if ($this->handle) $this->close();
|
2008-01-27 05:31:06 +00:00
|
|
|
}
|
2008-12-06 07:28:20 +00:00
|
|
|
|
2008-01-27 05:31:06 +00:00
|
|
|
}
|
2008-12-06 09:24:59 +00:00
|
|
|
|
|
|
|
// vim: et sw=4 sts=4
|