Source for file File.php

Documentation is available at File.php

  1. <?php
  2.  
  3. /**
  4.  * Represents a file in the filesystem
  5.  *
  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.
  9.  */
  10. {
  11.     
  12.     /** Filename of file this object represents */
  13.     protected $name;
  14.     
  15.     /** Handle for the file */
  16.     protected $handle = false;
  17.     
  18.     /** Instance of FSTools for interfacing with filesystem */
  19.     protected $fs;
  20.     
  21.     /**
  22.      * Filename of file you wish to instantiate.
  23.      * @note This file need not exist
  24.      */
  25.     public function __construct($name$fs false{
  26.         $this->name = $name;
  27.         $this->fs = $fs $fs FSTools::singleton();
  28.     }
  29.     
  30.     /** Returns the filename of the file. */
  31.     public function getName({return $this->name;}
  32.     
  33.     /** Returns directory of the file without trailing slash */
  34.     public function getDirectory({return $this->fs->dirname($this->name);}
  35.     
  36.     /**
  37.      * Retrieves the contents of a file
  38.      * @todo Throw an exception if file doesn't exist
  39.      */
  40.     public function get({
  41.         return $this->fs->file_get_contents($this->name);
  42.     }
  43.     
  44.     /** Writes contents to a file, creates new file if necessary */
  45.     public function write($contents{
  46.         return $this->fs->file_put_contents($this->name$contents);
  47.     }
  48.     
  49.     /** Deletes the file */
  50.     public function delete({
  51.         return $this->fs->unlink($this->name);
  52.     }
  53.     
  54.     /** Returns true if file exists and is a file. */
  55.     public function exists({
  56.         return $this->fs->is_file($this->name);
  57.     }
  58.     
  59.     /** Returns last file modification time */
  60.     public function getMTime({
  61.         return $this->fs->filemtime($this->name);
  62.     }
  63.     
  64.     /**
  65.      * Chmod a file
  66.      * @note We ignore errors because of some weird owner trickery due
  67.      *        to SVN duality
  68.      */
  69.     public function chmod($octal_code{
  70.         return @$this->fs->chmod($this->name$octal_code);
  71.     }
  72.     
  73.     /** Opens file's handle */
  74.     public function open($mode{
  75.         if ($this->handle$this->close();
  76.         $this->handle = $this->fs->fopen($this->name$mode);
  77.         return true;
  78.     }
  79.     
  80.     /** Closes file's handle */
  81.     public function close({
  82.         if (!$this->handlereturn false;
  83.         $status $this->fs->fclose($this->handle);
  84.         $this->handle = false;
  85.         return $status;
  86.     }
  87.     
  88.     /** Retrieves a line from an open file, with optional max length $length */
  89.     public function getLine($length null{
  90.         if (!$this->handle$this->open('r');
  91.         if ($length === nullreturn $this->fs->fgets($this->handle);
  92.         else return $this->fs->fgets($this->handle$length);
  93.     }
  94.     
  95.     /** Retrieves a character from an open file */
  96.     public function getChar({
  97.         if (!$this->handle$this->open('r');
  98.         return $this->fs->fgetc($this->handle);
  99.     }
  100.     
  101.     /** Retrieves an $length bytes of data from an open data */
  102.     public function read($length{
  103.         if (!$this->handle$this->open('r');
  104.         return $this->fs->fread($this->handle$length);
  105.     }
  106.     
  107.     /** Writes to an open file */
  108.     public function put($string{
  109.         if (!$this->handle$this->open('a');
  110.         return $this->fs->fwrite($this->handle$string);
  111.     }
  112.     
  113.     /** Returns TRUE if the end of the file has been reached */
  114.     public function eof({
  115.         if (!$this->handlereturn true;
  116.         return $this->fs->feof($this->handle);
  117.     }
  118.     
  119.     public function __destruct({
  120.         if ($this->handle$this->close();
  121.     }
  122.     
  123. }

Documentation generated on Thu, 19 Jun 2008 18:49:11 -0400 by phpDocumentor 1.4.2