Location: PHPKode > projects > PWF > PWF_0.3.0/File.php
<?php
/**
 * Common Files manipulation utilities
 * @copyright	Copyright (c) 2012, PWF
 * @license		http://phpwebframework.com/License 
 * @version PWF_0.3.0
 */
class _File
{
	/**
	 * Returns if the given file exists 
	 * @param string $fileName
	 * @return boolean
	 */
	public static function exists($fileName)
	{
		return file_exists($fileName);
	}
    
	/**
	 * Deletes the given file
	 * @param string $fileName
	 */
	public static function delete($fileName)
	{
	  unlink($fileName);
	}
	
	/**
	 * Retruns the paths of files that match the given fileName 
	 * @param string $fileName The file name or the pattern 
	 * @return array
	 */
	public static function match($fileName)
	{
		return glob($fileName);
	}

	/**
	 * Reads a file to a string
	 * @param string $fileName The file to be readed 
	 * @param integer $length The length of the string if none or null given it will read all the file 
	 * @throws _Exception_FileSystem
	 * @return string
	 */
	public static function read($fileName,$length = null)
	{
		if(self::exists($fileName))
		{
			$resource = @fopen($fileName, 'r');
			if(!$resource)
			{
				throw new _Exception_FileSystem(_Exception_Messages::$fileOpenFailure.":".$fileName, 000402);
			}
			else 
			{
				$result = ( $length == null ) ? @fread($resource, filesize($fileName)) : @fread($resource, $length);
				if(!$result)
				{
					throw new _Exception_FileSystem(_Exception_Messages::$fileReadFailure.":".$fileName, 000403);
				}
				
				$closed = fclose($resource);
				if(!$closed)
				{
					throw new _Exception_FileSystem(_Exception_Messages::$fileCloseFailure.":".$fileName, 000404);
				}
				return $result;
			}
		}
		else 
		{
			throw new _Exception_FileSystem(_Exception_Messages::$fileNotFound.":".$fileName, 000401);
		}
	}

	/**
	 * Reads file lines to array
	 * @param string $fileName
	 * @throws _Exception_FileSystem
	 * @return array
	 */
	public static function linesToArray($fileName)
	{
		$linesArray = array();
		if(file_exists($fileName))
		{ 
			$handle = @fopen($fileName, "r");
			if($handle)
			{
			  while (!feof($handle)) 
			  {
          $linesArray[] = fgets($handle, 4096);
        }	
        $closed = fclose($handle);
				if(!$closed)
				{
					throw new _Exception_FileSystem(_Exception_Messages::$fileCloseFailure.":".$fileName, 000407);
				}        
        unset($handle);
			}
			else 
			{
				throw new _Exception_FileSystem(_Exception_Messages::$fileReadFailure.":".$fileName, 000406);
			}
		}
		else 
		{
			throw new _Exception_FileSystem(_Exception_Messages::$fileNotFound.":".$fileName, 000405);
		}
		return $linesArray; 
	}	
	
	/**
	 * Writes a file with the given content and filename. 
	 * If the file exists the contents are replaced. 
	 * @param string $fileName
	 * @param string $content
	 */
	public static function write($fileName,$content)
	{
		$resource = fopen($fileName, 'w');
		fwrite($resource, $content);
		fclose($resource);
	}
	
	/**
	 * Appends content to a file that allready exists 
	 * @param string $fileName
	 * @param string $content
	 */
	public static function append($fileName,$content)
	{
		$resource = fopen($fileName, 'a');
		fwrite($resource, $content);
		fclose($resource);
	}	
}
?>
Return current item: PWF