diff --git a/config-templates/config.php b/config-templates/config.php
index 89605903b49bd2535c2fb6be172a31abe191d58c..1da08d2a11be5dc90cfc9249acb3ad04e8a66841 100644
--- a/config-templates/config.php
+++ b/config-templates/config.php
@@ -28,7 +28,14 @@ $config = array (
 	'certdir'               => 'cert/',
 	'dictionarydir'         => 'dictionaries/',
 	'loggingdir'            => 'log/',
-	
+
+	/*
+	 * A directory where simpleSAMLphp can save temporary files.
+	 *
+	 * SimpleSAMLphp will attempt to create this directory if it doesn't exist.
+	 */
+	'tempdir'               => '/tmp/simplesaml',
+
 	'version'				=>	'trunk',
 	
 	/**
diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index 95a9db9a120fb145028a69368189553166388107..b1d37a739046ca00c6b7dc204d202e4a7ec4afc4 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -2027,6 +2027,36 @@ class SimpleSAML_Utilities {
 		}
 	}
 
+
+	/**
+	 * Get temp directory path.
+	 *
+	 * This function retrieves the path to a directory where
+	 * temporary files can be saved.
+	 *
+	 * @return string  Path to temp directory, without a trailing '/'.
+	 */
+	public static function getTempDir() {
+
+		$globalConfig = SimpleSAML_Configuration::getInstance();
+
+		$tempDir = $globalConfig->getString('tempdir', '/tmp/simplesaml');
+
+		while (substr($tempDir, -1) === '/') {
+			$tempDir = substr($tempDir, 0, -1);
+		}
+
+		if (!is_dir($tempDir)) {
+			$ret = mkdir($tempDir, 0700, TRUE);
+			if (!$ret) {
+				throw new SimpleSAML_Error_Exception('Error creating temp dir ' .
+					var_export($tempDir, TRUE) . ': ' . SimpleSAML_Utilities::getLastError());
+			}
+		}
+
+		return $tempDir;
+	}
+
 }
 
 ?>
\ No newline at end of file