Blame view

class_mcFile.inc.php 5.56 KB
sn committed
1
<?php
Frederick committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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);
sn committed
29
//					fputs($var_file, mcCONST::LINEBREAK.mcCONST::LINEBREAK);
Frederick committed
30 31 32 33 34 35 36 37 38 39 40
				}
				fclose($var_file);
				return true;
			} catch (Exception $e) {
				mcException::handleException($e);
			}
		} else {
			return false;
		}
	}

41
	public static function getFileContent($param_filename, $param_asString = false) {
42 43 44 45 46 47 48 49
		if(is_readable($param_filename)) {
			try {
				$handle = fopen ($param_filename, "r");
				$array_content = array();
				while (!feof($handle)) {
					$array_content[] = fgets($handle);
				}
				fclose ($handle);
50 51 52 53 54 55 56 57 58 59 60
				
				if($param_asString) {
					$filecontent_string = '';
					foreach($array_content as $line) {
						$filecontent_string .= $line;
					}					
					return $filecontent_string;
				} else {
					return $array_content;
				}

61 62 63 64 65 66 67 68
			} catch (Exception $e) {
				mcException::handleException($e);
				return false;
			}
		} else {
			return Error::newError('DevError', 'file could not read', $param_code);
		}
	}
69 70

	public static function deleteFile($param_filename) {
sn committed
71
		D::ulli("mcFile::deleteFile($param_filename)");
72 73
		return unlink($param_filename);
	}
sn committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	
	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;
	   }
	}  	
	
sn committed
92
	/**
sn committed
93
	 * erstellt eine temporäre Datei, für weitere Dateimanipulatioin
sn committed
94
	 * 
sn committed
95
	 * @param array $param_outputfile @see mcFile::manipulateOutputfileArray($param_outputfile)
sn committed
96 97
	 * @return string
	 */
sn committed
98
	public static function createTmpFile(array $param_outputfile=null, $param_filepath=null) {
sn committed
99 100 101
		if(is_null($param_outputfile)) {
			$param_outputfile = array();
		}
sn committed
102
		mcFile::manipulateOutputfileArray($param_outputfile, $param_filepath);
sn committed
103 104 105 106 107
		
		$param_file = tempnam($param_outputfile['dirname'], $param_outputfile['prefix']);
		
		if($param_outputfile['filenameextension']) {
			$tmp_filename = $param_file.$param_outputfile['filenameextension'];
sn committed
108 109 110 111 112
			// @todo tempfile auch noch mal überprüfen, das es nicht existiert
			mcFile::rename($param_file, $tmp_filename);			
		}
		return $tmp_filename;
	}
sn committed
113 114 115 116 117 118 119 120
	
	/**
	 * 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
sn committed
121
	 * @todo filenameextension muessste extension heissen, so wie bei pathinfo()
sn committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
	 * 
	 * @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)) {
sn committed
137 138 139 140 141 142
			if(!is_null($param_filepath)) {
				$pathinfo = pathinfo($param_filepath);
				$param_outputfile['prefix'] = $pathinfo['filename'];
			} else {
				$param_outputfile['prefix'] = '';
			}
sn committed
143 144 145 146 147 148 149 150 151 152
		} 

		if(!array_key_exists('filename', $param_outputfile))  {
			$param_outputfile['filename'] = '';
		}
		
		if(!array_key_exists('filenameextension', $param_outputfile)) {
			$param_outputfile['filenameextension'] = '';
		} 		
		
sn committed
153
		
sn committed
154
	}
sn committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
	/**
	 * @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; 

	}	
	
Frederick committed
229 230 231
}

?>