filesystem
[ class tree: filesystem ] [ index: filesystem ] [ all elements ]

Source for file Files.class.php

Documentation is available at Files.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package filesystem
  9.  */
  10. if!defined"CLASSPATH" ) )
  11. {
  12.   echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
  13.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
  14.   exit();
  15. }
  16.  
  17. /**
  18.  *
  19.  */
  20. include_onceCLASSPATH."filesystem/FilesystemToolkit.class.php" );
  21.  
  22. /**
  23.  * A class to manage files in a filesystemtree.
  24.  *
  25.  * @package   filesystem
  26.  * @version   0.2.26
  27.  * @author    Daniel Plücken <daniel@debakel.net>
  28.  * @license   http://www.gnu.org/copyleft/lesser.html
  29.  *             GNU Lesser General Public License
  30.  * @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
  31.  *
  32.  *  This library is free software; you can redistribute it and/or
  33.  *  modify it under the terms of the GNU Lesser General Public
  34.  *  License as published by the Free Software Foundation; either
  35.  *  version 2.1 of the License.
  36.  *
  37.  *  This library is distributed in the hope that it will be useful,
  38.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  39.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40.  *  GNU Lesser General Public License for more details.
  41.  *
  42.  *  You should have received a copy of the GNU Lesser General
  43.  *  Public License along with this library; if not, write to the
  44.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  45.  *  Boston, MA 02111-1307 USA
  46.  */
  47. class Files
  48. {
  49.   /**
  50.    * @access public
  51.    * @var    string $path 
  52.    */
  53.   var $path         = "";
  54.   /**
  55.    * @access private
  56.    * @var    Folder  $parentfolder 
  57.    */
  58.   var $parentfolder = "";
  59.   /**
  60.    * @access public
  61.    * @var    string $filename 
  62.    */
  63.   var $filename     = "";
  64.   /**
  65.    * @access public
  66.    * @var    integer $filename 
  67.    */
  68.   var $filesize     = 0;
  69.   /**
  70.    * @access public
  71.    * @var    string $content 
  72.    */
  73.   var $content      = "";
  74.   /**
  75.    * Is set if the function saveTmp() has been invoke.
  76.    *
  77.    * @access private
  78.    * @var    string $tmp_fh 
  79.    */
  80.   var $tmp_fh       = "";
  81.  
  82.  
  83.  
  84.  
  85.   /**
  86.    * Constructor
  87.    *
  88.    * @version 1.0
  89.    * @since   0.1.0
  90.    * @author  Daniel Plücken <daniel@debakel.net>
  91.    * @access  public
  92.    * @param   string  $path_and_filename 
  93.    * @or
  94.    * @param   string  $path 
  95.    * @param   string  $filename 
  96.    */
  97.   function Files()
  98.   {
  99.       $param func_get_args();
  100.  
  101.       ifempty$param[1) )
  102.       {
  103.         $tmpArr FilesystemToolkit
  104.                 ::separatePathAndFile$param[0);
  105.         $path $tmpArr[0];
  106.         $file $tmpArr[1];
  107.       }
  108.       else
  109.       {
  110.         $path $param[0];
  111.         $file $param[1];
  112.       }
  113.  
  114.       $this->path     = $path;
  115.       $this->filename = $file;
  116.   }
  117.  
  118.  
  119.  
  120.   /**
  121.    * Compares the given Files object with this one. If both equal each other it
  122.    * will be returned true, otherwise false. The two file objects will be
  123.    * accounted as equal, if the the filenames and pathes match each other.
  124.    *
  125.    * @version 1.0
  126.    * @since   0.2.22
  127.    * @author  Daniel Plücken <daniel@debakel.net>
  128.    * @access  public
  129.    * @param   ImageFiles $img_obj 
  130.    * @return  boolean 
  131.    */
  132.   function equals&$img_obj )
  133.   {
  134.     return $this->filename == $img_obj->filename
  135.         && $this->path == $img_obj->path;
  136.   }
  137.  
  138.  
  139.  
  140.  
  141.   /**
  142.    * Returns the path to the file of this file object.
  143.    *
  144.    * @version 1.0
  145.    * @since   0.1.0
  146.    * @author  Daniel Plücken <daniel@debakel.net>
  147.    * @access  public
  148.    * @return  string 
  149.    */
  150.   function getPath()
  151.   return $this->path}
  152.  
  153.  
  154.  
  155.  
  156.   /**
  157.    * Returns the filename of this file object.
  158.    *
  159.    * @version 1.0
  160.    * @since   0.1.0
  161.    * @author  Daniel Plücken <daniel@debakel.net>
  162.    * @access  public
  163.    * @return  string 
  164.    */
  165.   function getName()
  166.   return $this->filename}
  167.  
  168.  
  169.  
  170.   /**
  171.    * Returns the file extension of the given filename.
  172.    *
  173.    * @static
  174.    * @version 1.0
  175.    * @since   0.2.2
  176.    * @author  Daniel Plücken <daniel@debakel.net>
  177.    * @access  public
  178.    * @param   string 
  179.    * @return  string|false
  180.    */
  181.   function getExtension$filename )
  182.   {
  183.     if!preg_match"!\.!"$filename ) )
  184.       return false;
  185.     else
  186.       return preg_replace"!^.+\.([^.]+)$i!""$1"$filename );
  187.   }
  188.  
  189.  
  190.  
  191.   /**
  192.    * Returns the content of this file object.
  193.    *
  194.    * @version 1.0
  195.    * @since   0.1.0
  196.    * @author  Daniel Plücken <daniel@debakel.net>
  197.    * @access  public
  198.    * @return  string 
  199.    */
  200.   function &getContent()
  201.   {
  202.     if empty$this->content ) )
  203.        $this->load();
  204.  
  205.     return $this->content;
  206.   }
  207.  
  208.  
  209.  
  210.  
  211.   /**
  212.    * Returns the content of this file object. This function is an alias for
  213.    * the function getContent().
  214.    *
  215.    * @version 1.0
  216.    * @since   0.1.4
  217.    * @author  Daniel Plücken <daniel@debakel.net>
  218.    * @access  public
  219.    * @return  string 
  220.    */
  221.   function get()
  222.   return $this->getContent()}
  223.  
  224.  
  225.  
  226.  
  227.   /**
  228.    * Sets the parent folder of this file object that there is a back reference
  229.    * up to the root of the filesystem tree,
  230.    *
  231.    * @version 1.0
  232.    * @since   0.1.0
  233.    * @author  Daniel Plücken <daniel@debakel.net>
  234.    * @access  protected
  235.    * @param   Folder     $objRef  The reference to the folder object that
  236.    *                               contains this file.
  237.    * @return  void 
  238.    */
  239.   function setParentFolder&$objRef )
  240.   $this->parentfolder = &$objRef}
  241.  
  242.  
  243.  
  244.  
  245.   /**
  246.    * Sets the content of this file object.
  247.    *
  248.    * @version 1.0
  249.    * @since   0.1.0
  250.    * @author  Daniel Plücken <daniel@debakel.net>
  251.    * @access  public
  252.    * @param   string  $string  The content which should be set to the file.
  253.    * @return  void 
  254.    */
  255.   function setContent$string )
  256.   {
  257.      if !empty$string ) )
  258.         $this->content = $string;
  259.   }
  260.  
  261.  
  262.  
  263.  
  264.   /**
  265.    * Sets the content of this file object.
  266.    *
  267.    * @version 1.0
  268.    * @since   0.1.2
  269.    * @author  Daniel Plücken <daniel@debakel.net>
  270.    * @access  public
  271.    * @param   integer  $filesize  The size of this file to be set.
  272.    * @return  void 
  273.    */
  274.   function setFilesize$filesize )
  275.   {
  276.      if preg_match"!^\d+$!"$filesize ) )
  277.         $this->filesize = $filesize;
  278.   }
  279.  
  280.  
  281.  
  282.  
  283.   /**
  284.    * Sets the content of this file object.
  285.    *
  286.    * @version 1.1
  287.    * @since   0.1.2
  288.    * @author  Daniel Plücken <daniel@debakel.net>
  289.    * @access  public
  290.    * @return  integer 
  291.    */
  292.   function getFilesize()
  293.   {
  294.     if !empty$this->filesize ) )
  295.        return $this->filesize;
  296.     else
  297.        return sprintf"%u"filesize$this->path."/".$this->filename ) );
  298.   }
  299.  
  300.  
  301.  
  302.  
  303.   /**
  304.    * Loads the content of this file object from the filesystem and sets
  305.    * the content to this object.
  306.    *
  307.    * @version 1.01
  308.    * @since   0.1.0
  309.    * @author  Daniel Plücken <daniel@debakel.net>
  310.    * @access  public
  311.    * @return  void 
  312.    */
  313.   function load()
  314.   {
  315.       $this->setContent(
  316.                  FilesystemToolkit
  317.                ::loadFileContent$this->path$this->filename )
  318.                        );
  319.   }
  320.  
  321.  
  322.  
  323.  
  324.   /**
  325.    * Saves the file of this object with given name.
  326.    * You need to give in the filename at least.
  327.    *
  328.    * @version 1.12
  329.    * @since   0.1.0
  330.    * @author  Daniel Plücken <daniel@debakel.net>
  331.    * @access  public
  332.    * @param   string  $path  Where the file should be stored.
  333.    * @param   string  $file  Which name the file should get.
  334.    * @return  boolean 
  335.    */
  336.   function saveAs()
  337.   {
  338.       $param func_get_args();
  339.  
  340.       ifempty$param[1) )
  341.       {
  342.         $tmpArr FilesystemToolkit
  343.                 ::separatePathAndFile$param[0);
  344.         $path $tmpArr[0];
  345.         $file $tmpArr[1];
  346.       }
  347.       else
  348.       {
  349.         $path $param[0];
  350.         $file $param[1];
  351.       }
  352.  
  353.       if(
  354.           empty$path )
  355.        && empty$file )
  356.         )
  357.       {
  358.         echo 'You have to give a filename at least.\n<br>';
  359.         return false;
  360.       }
  361.       else
  362.       {
  363.         $this->path = $path;
  364.         $this->filename = $file;
  365.  
  366.         return fwrite(
  367.                        fopen$path."/".$file'w' ),
  368.                        $this->get()
  369.                      );
  370.       }
  371.   }
  372.  
  373.  
  374.  
  375.  
  376.   /**
  377.    * Saves the file of this object.
  378.    *
  379.    * @version 1.01
  380.    * @since   0.1.1
  381.    * @author  Daniel Plücken <daniel@debakel.net>
  382.    * @access  public
  383.    * @return  void 
  384.    */
  385.   function save()
  386.   $this->saveAs$this->path$this->filename )}
  387.  
  388.  
  389.  
  390.   /**
  391.    * Saves the file of this object in a temporary file.
  392.    *
  393.    * @access  public
  394.    * @version 1.02
  395.    * @since   0.2.0
  396.    *
  397.    * @return  filehandle 
  398.    */
  399.   function saveTmp()
  400.   {
  401.       $this->tmp_fh = tmpfile();
  402.       if !$this->tmp_fh )
  403.       {   $this->tmp_fh = tempnamgetcwd()"GilliGan_" );  }
  404.  
  405.       if ( ($out fwrite$this->tmp_fh$this->get() )) !== false )
  406.       {   fseek$this->tmp_fh)}
  407.  
  408.       return $out;
  409.   }
  410.  
  411.  
  412.  
  413.   /**
  414.    * Deletes the temporary file of this object.
  415.    *
  416.    * @version 1.0
  417.    * @since   0.2.0
  418.    * @author  Daniel Plücken <daniel@debakel.net>
  419.    * @access  public
  420.    * @return  boolean 
  421.    */
  422.   function deleteTmp()
  423.   {
  424.     fclose$this->tmp_fh );
  425.     clearstatcache();
  426.   }
  427. // End of class Files
  428. ?>

Documentation generated on Thu, 05 Jun 2008 19:11:27 +0200 by phpDocumentor 1.4.1