class_mcFile.inc.php 5.56 KB
<?php
class mcFile {

	/**
	 *
	 *
	 * @param string $param_filename
	 * @param string $param_text
	 * @param string $param_kindof -- r|r+|w|w+|a|a+ @see http://de3.php.net/manual/de/function.fopen.php
	 *					r - ptr at the beginning
	 *					w - ptr at the beginning and file size 0
	 *					a - ptr at the end
	 * @return bool
	 */
	public static function write($param_filename, $param_text, $param_kindof = null) {
		
		if($param_filename && $param_text) {
			
			if($param_kindof === null) {
				$param_kindof = 'a';
			}
			try {
				@$var_file = fopen($param_filename,$param_kindof);

				if ($var_file === false) {
					return false;
				} else {
					fputs($var_file, $param_text);
//					fputs($var_file, mcCONST::LINEBREAK.mcCONST::LINEBREAK);
				}
				fclose($var_file);
				return true;
			} catch (Exception $e) {
				mcException::handleException($e);
			}
		} else {
			return false;
		}
	}

	public static function getFileContent($param_filename, $param_asString = false) {
		if(is_readable($param_filename)) {
			try {
				$handle = fopen ($param_filename, "r");
				$array_content = array();
				while (!feof($handle)) {
					$array_content[] = fgets($handle);
				}
				fclose ($handle);
				
				if($param_asString) {
					$filecontent_string = '';
					foreach($array_content as $line) {
						$filecontent_string .= $line;
					}					
					return $filecontent_string;
				} else {
					return $array_content;
				}

			} catch (Exception $e) {
				mcException::handleException($e);
				return false;
			}
		} else {
			return Error::newError('DevError', 'file could not read', $param_code);
		}
	}

	public static function deleteFile($param_filename) {
		D::ulli("mcFile::deleteFile($param_filename)");
		return unlink($param_filename);
	}
	
	public static function rename($param_from, $param_to){
	   //file_exists() - Existenz der Datei prüfen
	   if(!file_exists($param_to)){
		   return rename($param_from, $param_to);
//		  //copy() - Datei kopieren
//		  if (!copy($from, $to)) {
//			 print ("failed to copy $file...<br>\n");
//		  }
//		  else{
//			 //unlink() - Datei löschen
//			 unlink($from);
//		  }
	   } else {
		   return false;
	   }
	}  	
	
	/**
	 * erstellt eine temporäre Datei, für weitere Dateimanipulatioin
	 * 
	 * @param array $param_outputfile @see mcFile::manipulateOutputfileArray($param_outputfile)
	 * @return string
	 */
	public static function createTmpFile(array $param_outputfile=null, $param_filepath=null) {
		if(is_null($param_outputfile)) {
			$param_outputfile = array();
		}
		mcFile::manipulateOutputfileArray($param_outputfile, $param_filepath);
		
		$param_file = tempnam($param_outputfile['dirname'], $param_outputfile['prefix']);
		
		if($param_outputfile['filenameextension']) {
			$tmp_filename = $param_file.$param_outputfile['filenameextension'];
			// @todo tempfile auch noch mal überprüfen, das es nicht existiert
			mcFile::rename($param_file, $tmp_filename);			
		}
		return $tmp_filename;
	}
	
	/**
	 * manipuliert $param_outputfile so das Informationen für die zuerstellende Datei 
	 * für Dateimanipulationen vorliegen
	 * 
	 * @todo es könnte überprüft werden ob $param_outputfile['dirname'] schreibbar ist
	 * @todo es könnte überprüft werden ob $param_outputfile['filename'] schreibbar ist
	 * @todo filepath könnte alle anderen Werte generieren
	 * @todo filenameextension muessste extension heissen, so wie bei pathinfo()
	 * 
	 * @param array $param_outputfile - array('dirname'=>,'prefix'=>,'filename'=>,'filenameextension'=>, )
	 * @param type $param_filepath - generiert bis jetzt nur dirname
	 */
	public static function manipulateOutputfileArray(array &$param_outputfile, $param_filepath=null) {
		if(!array_key_exists('dirname', $param_outputfile)) {
			if(!is_null($param_filepath)) {
				$path_parts = pathinfo($param_filepath);
				$param_outputfile['dirname'] = $path_parts['dirname'].'/';				
			} else {
				$param_outputfile['dirname'] = '/tmp/';
			}				
		}		
		
		if(!array_key_exists('prefix', $param_outputfile)) {
			if(!is_null($param_filepath)) {
				$pathinfo = pathinfo($param_filepath);
				$param_outputfile['prefix'] = $pathinfo['filename'];
			} else {
				$param_outputfile['prefix'] = '';
			}
		} 

		if(!array_key_exists('filename', $param_outputfile))  {
			$param_outputfile['filename'] = '';
		}
		
		if(!array_key_exists('filenameextension', $param_outputfile)) {
			$param_outputfile['filenameextension'] = '';
		} 		
		
		
	}
	/**
	 * @see http://www.selfphp.info/kochbuch/kochbuch.php?code=61
	 * 
	 * @param type $size
	 * @param type $praefix
	 * @param type $short
	 * @return string
	 */
	function binary_multiples($size, $praefix=true, $short= true)
	{

		if($praefix === true)
		{
			if($short === true)
			{
				$norm = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 
							  'EB', 'ZB', 'YB');
			}
			else
			{
				$norm = array('Byte', 
							  'Kilobyte', 
							  'Megabyte', 
							  'Gigabyte', 
							  'Terabyte', 
							  'Petabyte', 
							  'Exabyte', 
							  'Zettabyte', 
							  'Yottabyte'
							 );
			}

			$factor = 1000;
		}
		else
		{
			if($short === true)
			{
				$norm = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 
							  'EiB', 'ZiB', 'YiB');
			}
			else
			{
				$norm = array('Byte', 
							  'Kibibyte', 
							  'Mebibyte', 
							  'Gibibyte', 
							  'Tebibyte', 
							  'Pebibyte', 
							  'Exbibyte', 
							  'Zebibyte', 
							  'Yobibyte'
							 );
			}

			$factor = 1024;

		}

		$count = count($norm) -1;

		$x = 0;
		while ($size >= $factor && $x < $count) 
		{ 
			$size /= $factor; 
			$x++;
		} 

	  $size = sprintf("%01.2f", $size) . ' ' . $norm[$x];

		return $size; 

	}	
	
}

?>