class_mcFile.inc.php 2.6 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) {
		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;
	   }
	}  	
	
	/**
	 * 
	 * @param string $param_filepostfix exampe: .pdf, .txt, .csv
	 * @param string $param_prefix
	 * @return string
	 */
	public static function createTmpFile($param_filepostfix='.tmp', $param_prefix='tmp_', $param_dir='/tmp/') {
		$param_file = tempnam($param_dir, $param_prefix);
		if($param_filepostfix) {
			$tmp_filename = $param_file.$param_filepostfix;
			// @todo tempfile auch noch mal überprüfen, das es nicht existiert
			mcFile::rename($param_file, $tmp_filename);			
		}
		return $tmp_filename;
	}
}

?>