[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/file/ -> file.class.php (source)

   1  <?php
   2  
   3      /**
   4       * \defgroup File
   5       *
   6       * The File module provides some classes to deal with files. The File class
   7       * is the most useful once, since provides OOP wrappers around some PHP
   8       * functions that work with files.
   9       */
  10  
  11  
  12       
  13       lt_include( PLOG_CLASS_PATH."class/misc/glob.class.php" );
  14  
  15       define( "FILE_DEFAULT_DIRECTORY_CREATION_MODE", 0700 );
  16  
  17      /**
  18       * \ingroup File
  19       *
  20       * Encapsulation of a class to manage files. It is basically a wrapper
  21       * to some of the php functions.
  22       * http://www.php.net/manual/en/ref.filesystem.php
  23       */
  24       class File  
  25       {
  26  
  27           var $_fileName;
  28           var $_handler;
  29           var $_mode;
  30           
  31           /**
  32            * Creates an object of this type, but the file is not automaticaly 
  33            * opened
  34            *
  35            * @param fileName The name of the file
  36            */
  37  		 function File( $fileName )
  38           {
  39               
  40               
  41               $this->_fileName = $fileName;
  42           }
  43           
  44           /**
  45            * Opens the file in the specified mode
  46            * http://www.php.net/manual/en/function.fopen.php
  47            * Mode by default is 'r' (read only)
  48            * Returns 'false' if operation failed
  49            *
  50            * @param mode The mode in which the file is opened
  51            * @return false if the operation failed
  52            */
  53  		 function open( $mode = "r" )
  54           {
  55               $this->_handler = fopen( $this->_fileName, $mode );
  56               
  57               $this->_mode = $mode;
  58               
  59               return $this->_handler;
  60           }
  61           
  62           /**
  63            * Closes the stream currently being held by this object
  64            *
  65            * @return nothing
  66            */
  67  		 function close()
  68           {
  69               fclose( $this->_handler );
  70           }
  71           
  72           /**
  73            * Reads the whole file and put it into an array, where every position
  74            * of the array is a line of the file (new-line characters not
  75            * included)
  76            *
  77            * @return An array where every position is a line from the file.
  78            */
  79  		 function readFile()
  80           {
  81               $contents = Array();
  82               
  83               $contents = file( $this->_fileName );
  84               
  85               for( $i = 0, $elements = count( $contents ); $i < $elements; $i++ )
  86                   $contents[$i] = trim( $contents[$i] );
  87               
  88               return $contents;
  89           }
  90           
  91           /**
  92            * Reads bytes from the currently opened file
  93            *
  94            * @param size Amount of bytes we'd like to read from the file. It is 
  95            * set to 4096 by default.
  96            * @return Returns the read contents
  97            */
  98  		 function read( $size = 4096 )
  99           {
 100               return( fread( $this->_handler, $size ));
 101           }
 102           
 103           /**
 104            * checks whether we've reached the end of file
 105            *
 106            * @return True if we reached the end of the file or false otherwise
 107            */
 108  		 function eof()
 109           {
 110               return feof( $this->_handler );
 111           }
 112           
 113           /**
 114            * Writes data to disk
 115            *
 116            * @param data The data that we'd like to write to disk
 117            * @return returns the number of bytes written, or false otherwise
 118            */
 119  		 function write( $data )
 120           {
 121               return fwrite( $this->_handler, $data );
 122           }
 123           
 124           /**
 125            * truncates the currently opened file to a given length
 126            *
 127            * @param length Lenght at which we'd like to truncate the file
 128            * @return true if successful or false otherwise
 129            */
 130  		 function truncate( $length = 0 )
 131           {
 132               return ftruncate( $this->_handler, $length );
 133           }
 134           
 135           /**
 136            * Writes an array of text lines to the file.
 137            *
 138            * @param lines The array with the text.
 139            * @return Returns true if successful or false otherwise.
 140            */
 141  		 function writeLines( $lines )
 142           {
 143               // truncate the file to remove the old contents
 144               $this->truncate();
 145               
 146               foreach( $lines as $line ) {
 147                   //print("read: \"".htmlentities($line)."\"<br/>");
 148                   if( !$this->write( $line, strlen($line))) {
 149                       return false;
 150                   }
 151                   /*else
 152                      print("written: \"".htmlentities($line)."\"<br/>");*/
 153               }
 154               
 155               return true;
 156           }
 157           
 158           /**
 159            * Returns true wether the file is a directory. See
 160            * http://fi.php.net/manual/en/function.is-dir.php for more details.
 161            *
 162            * @param file The filename we're trying to check. If omitted, the
 163            * current file will be used (note that this function can be used as
 164            * static as long as the file parameter is provided)
 165            * @return Returns true if the file is a directory.
 166            */
 167  		 function isDir( $file = null )
 168           {
 169               if( $file == null )
 170                   $file = $this->_fileName;
 171               
 172               return is_dir( $file );
 173           }
 174           
 175           /**
 176            * Returns true if the file is writable by the current user.
 177            * See http://fi.php.net/manual/en/function.is-writable.php for more 
 178            * details.
 179            *
 180            * @param file The filename we're trying to check. If omitted, the
 181            * current file will be used (note that this function can be used as
 182            * static as long as the file parameter is provided)
 183            * @return Returns true if the file is writable, or false otherwise.
 184            */
 185  		 function isWritable( $file = null )
 186           {
 187               if( $file == null )
 188                   $file = $this->_fileName;
 189               
 190               return is_writable( $file );
 191           }
 192           
 193           /**
 194            * returns true if the file is readable. Can be used as static if a
 195            * filename is provided
 196            *
 197            * @param if provided, this method can be used as an static method and
 198            * it will check for the readability status of the file
 199            * @return true if readable or false otherwise
 200            */
 201  		 function isReadable( $file = null )
 202           {
 203               if( $file == null )
 204                   $file = $this->_fileName;
 205                   
 206              clearstatcache();
 207               
 208               return is_readable( $file );
 209           }
 210           
 211           /**
 212            * removes a file. Can be used as static if a filename is provided. 
 213            * Otherwise it will remove the file that was given in the constructor
 214            *
 215            * @param optionally, name of the file to delete
 216            * @return True if successful or false otherwise
 217            */
 218  		 function delete( $file = null )
 219           {
 220               if( $file == null )
 221                   $file = $this->_fileName;
 222  
 223              if( !File::isReadable( $file ))
 224                  return false;
 225               
 226               if( File::isDir( $file ))
 227                   $result = @rmdir( $file );
 228               else
 229                   $result = @unlink( $file );
 230               
 231               return $result;
 232           }
 233           
 234           /**
 235            * removes a directory, optinally in a recursive fashion
 236            *
 237            * @param dirName
 238            * @param recursive Whether to recurse through all subdirectories that
 239            * are within the given one and remove them.
 240            * @param onlyFiles If the recursive mode is enabled, setting this to 'true' will
 241            * force the method to only remove files but not folders. The directory will not be
 242            * removed but all the files included it in (and all subdirectories) will be.
 243            * @param excludedFiles If some files or directories should not be removed (like .htaccess) they can be added
 244            * to this array. This operation is case sensitive!
 245            * @return True if successful or false otherwise
 246            * @static
 247            */
 248  		 function deleteDir( $dirName, $recursive = false, $onlyFiles = false, $excludedFiles = array('') )
 249           {
 250              // if the directory can't be read, then quit with an error
 251              if( !File::isReadable( $dirName ) || !File::exists( $dirName )) {
 252                  return false;
 253              }
 254           
 255              // if it's not a directory, let's get out of here and transfer flow
 256              // to the right place...
 257              if( !File::isDir( $dirName )) {
 258                  return File::delete( $dirName );
 259              }
 260                  
 261              // Glob::myGlob is easier to use than Glob::glob, specially when 
 262              // we're relying on the native version... This improved version 
 263              // will automatically ignore things like "." and ".." for us, 
 264              // making it much easier!
 265              $files = Glob::myGlob( $dirName, "*" );
 266              foreach( $files as $file ) 
 267              {
 268                  // check if the filename is in the list of files we must not delete
 269                  if( File::isDir( $file ) && array_search(basename( $file ), $excludedFiles) === false) {
 270                      // perform a recursive call if we were allowed to do so
 271                      if( $recursive ) {
 272                          File::deleteDir( $file, $recursive, $onlyFiles, $excludedFiles );
 273                      }
 274                  }
 275  
 276                  // check if the filename is in the list of files we must not delete
 277                  if(array_search(basename( $file ), $excludedFiles) === false) {
 278                      // File::delete can remove empty folders as well as files
 279                      if( File::isReadable( $file )) {
 280                          File::delete( $file );
 281                      }
 282                  }
 283              }
 284              
 285              // finally, remove the top-level folder but only in case we
 286              // are supposed to!
 287              if( !$onlyFiles ) {
 288                  File::delete( $dirName );
 289              }
 290              
 291              return true;
 292           }
 293           
 294           /**
 295               * Creates a new folder. If the folder name is /a/b/c and neither 
 296               * /a or /a/b exist, this method will take care of creating the 
 297               * whole folder structure automatically.
 298            *
 299            * @static
 300            * @param dirName The name of the new folder
 301            * @param mode Attributes that will be given to the folder
 302            * @return Returns true if no problem or false otherwise.
 303            */
 304  		 function createDir( $dirName, 
 305                               $mode = FILE_DEFAULT_DIRECTORY_CREATION_MODE )
 306           {
 307               if(File::exists($dirName)) return true;
 308  
 309               if(substr($dirName, strlen($dirName)-1) == "/" ){
 310                   $dirName = substr($dirName, 0,strlen($dirName)-1);
 311               }
 312  
 313               // for example, we will create dir "/a/b/c"
 314               // $firstPart = "/a/b"
 315               $firstPart = substr($dirName,0,strrpos($dirName, "/" ));           
 316  
 317               if(file_exists($firstPart)){
 318                   if(!mkdir($dirName,$mode)) return false;
 319                   chmod( $dirName, $mode );
 320               } else {
 321                   File::createDir($firstPart,$mode);
 322                   if(!mkdir($dirName,$mode)) return false;
 323                   chmod( $dirName, $mode );
 324               }
 325               
 326               return true;
 327           }
 328          
 329           /**
 330            * Change the current directory
 331            *
 332            * @param dir
 333            */
 334  		 function chDir( $dir )
 335           {
 336               return( chdir( $dir ));
 337           }
 338           
 339           /**
 340            * returns a temporary filename in a pseudo-random manner
 341            *
 342            * @return a temporary name
 343            */
 344  		 function getTempName()
 345           {
 346               return md5(microtime());
 347           }
 348           
 349           /**
 350            * Returns the size of the file.
 351            *
 352            * @param string fileName An optional parameter specifying the name 
 353            * of the file. If omitted, we will use the file that we have 
 354            * currently opened. Please note that this function can
 355            * be used as static if a file name is specified.
 356            * @return An integer specifying the size of the file.
 357            */
 358  		 function getSize( $fileName = null )
 359           {
 360               if( $fileName == null )
 361                   $fileName = $this->_fileName;
 362               
 363               $size = filesize( $fileName );
 364               if( !$size )
 365                   return -1;
 366               else
 367                   return $size;
 368           }
 369           
 370           /**
 371            * renames a file
 372            *
 373            * http://www.php.net/manual/en/function.rename.php
 374            *
 375            * This function can be used as static if inFile and outFile are both 
 376            * not empty. if outFile is empty, then the internal file of the
 377            * current object will be used as the input file and the first 
 378            * parameter of this method will become the destination file name.
 379            *
 380            * @param inFile Original file
 381            * @param outFile Destination file.
 382            * @return Returns true if file was renamed ok or false otherwise.
 383            */
 384            function rename( $inFile, $outFile = null )
 385            {
 386                // check how many parameters we have
 387                if( $outFile == null ) {
 388                    $outFile = $inFile;
 389                    $inFile  = $this->_fileName;
 390                }
 391  
 392                // Checkt the $inFile and $outFile are the same file or not
 393                if ( realpath( dirname( $inFile ) ) == realpath( dirname( $outFile ) ) &&
 394                     basename( $inFile ) == basename( $outFile ) )
 395                    return true;
 396                                
 397                // In order to work around the bug in php versions older
 398                // than 4.3.3, where rename will not work across different
 399                // partitions, this will be a copy and delete of the original file
 400              
 401                // copy the file to the new location
 402                if (!copy($inFile, $outFile)) {
 403                    // The copy failed, return false
 404                    return false;
 405                }
 406              
 407                // Now delete the old file
 408                // NOTICE, we are not checking the error here.  It is possible 
 409                // the the original file will remain and the copy will exist.
 410                //
 411                // One way to potentially fix this is to look at the result of
 412                // unlink, and then delete the copy if unlink returned FALSE, 
 413                // but this call to unlink could just as easily fail
 414                unlink( $inFile );
 415              
 416                return true;;
 417            }
 418           
 419           /**
 420            * copies a file from one place to another.
 421            * This method is always static
 422            *
 423            * @param inFile
 424            * @param destFile
 425            * @return True if successful or false otherwise
 426            * @static
 427            */
 428  		 function copy( $inFile, $outFile )
 429           {
 430               return @copy( $inFile, $outFile );
 431           }
 432           
 433           /**
 434            * changes the permissions of a file, via PHP's chmod() function
 435            *
 436            * @param inFile The name of the file whose mode we'd like to change
 437            * @param mode The new mode, or if none provided, it will default 
 438            * to 0644
 439            * @return true if successful or false otherwise 
 440            * @static
 441            */
 442  		 function chMod( $inFile, $mode = 0644 )
 443           {
 444               return chmod( $inFile, $mode );
 445           }
 446           
 447           /**
 448            * returns true if the file exists.
 449            *
 450            * Can be used as an static method if a file name is provided as a
 451            *  parameter
 452            * @param fileName optinally, name of the file whose existance we'd
 453            * like to check
 454            * @return true if successful or false otherwise
 455            */
 456  		 function exists( $fileName = null ) 
 457           {
 458               if( $fileName == null )
 459                   $fileName = $this->_fileName;
 460                   
 461              clearstatcache();
 462               
 463               return file_exists( $fileName );
 464           } 
 465  
 466           /** 
 467            * returns true if the file could be touched
 468            *
 469            * Can be used to create a file or to reset the timestamp.
 470            * @return true if successful or false otherwise
 471            * @see PHP Function touch()
 472            *
 473            */
 474           function touch( $fileName = null )
 475           {
 476               if( $fileName == null )
 477                   return false;
 478  
 479               return touch($fileName);
 480           }
 481  
 482           /** 
 483            * returns the basename of a file
 484            *
 485            * @return basename of the file
 486            * @see PHP Function basename()
 487            *
 488            */
 489           function basename( $fileName = null )
 490           {
 491               if( $fileName == null )
 492                   return false;
 493  
 494               $basename = preg_replace( '/^.+[\\\\\/]/', '', $fileName );
 495  
 496               return $basename;
 497           }         
 498       }
 499  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics