--- graph_export.orig.php	2006-01-04 11:08:30.000000000 +0800
+++ graph_export.php	2006-09-11 12:42:05.849248184 +0800
@@ -308,13 +308,18 @@
 	}
 
 	/* delete all files and directories in the cacti_export_path */
-	del_directory($cacti_export_path, false);
+	//del_directory($cacti_export_path, false);
 
 	/* test how will the export will be made */
 	if (read_config_option('export_presentation') == 'tree') {
+		/* delete all files and directories in the cacti_export_path */
+		del_directory($cacti_export_path, false);
 		export_log("Running graph export with tree organization");
 		$total_graphs_created = tree_export();
 	}else {
+		/* delete all files and directories in the cacti_export_path */
+		del_directory($cacti_export_path.'/tmp', false);
+		@mkdir($cacti_export_path.'/tmp');
 		/* copy the css/images on the first time */
 		if (file_exists("$cacti_export_path/main.css") == false) {
 			copy("$cacti_root_path/include/main.css", "$cacti_export_path/main.css");
@@ -325,10 +330,12 @@
 		}
 
 		/* if the index file already exists, delete it */
-		check_remove($cacti_export_path . "/index.html");
+		//check_remove($cacti_export_path . "/index.html");
+		check_remove($cacti_export_path . "/tmp/index.html");
 
 		/* open pointer to the new index file */
-		$fp_index = fopen($cacti_export_path . "/index.html", "w");
+		//$fp_index = fopen($cacti_export_path . "/index.html", "w");
+		$fp_index = fopen($cacti_export_path . "/tmp/index.html", "w");
 
 		/* get a list of all graphs that need exported */
 		$graphs = db_fetch_assoc("select
@@ -364,20 +371,24 @@
 		$i = 0; $k = 0;
 		if ((sizeof($graphs) > 0) && (sizeof($rras) > 0)) {
 		foreach ($graphs as $graph) {
-			check_remove($cacti_export_path . "/thumb_" . $graph["local_graph_id"] . ".png");
-			check_remove($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html");
+			//check_remove($cacti_export_path . "/thumb_" . $graph["local_graph_id"] . ".png");
+			//check_remove($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html");
+			check_remove($cacti_export_path . "/tmp/thumb_" . $graph["local_graph_id"] . ".png");
+			check_remove($cacti_export_path . "/tmp/graph_" . $graph["local_graph_id"] . ".html");
 
 			/* settings for preview graphs */
 			$graph_data_array["graph_height"] = "100";
 			$graph_data_array["graph_width"] = "300";
 			$graph_data_array["graph_nolegend"] = true;
 			$graph_data_array["export"] = true;
-			$graph_data_array["export_filename"] = "thumb_" . $graph["local_graph_id"] . ".png";
+			//$graph_data_array["export_filename"] = "thumb_" . $graph["local_graph_id"] . ".png";
+			$graph_data_array["export_filename"] = "tmp/thumb_" . $graph["local_graph_id"] . ".png";
 			rrdtool_function_graph($graph["local_graph_id"], 0, $graph_data_array, $rrdtool_pipe);
 			$total_graphs_created++;
 
 			/* generate html files for each graph */
-			$fp_graph_index = fopen($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html", "w");
+			//$fp_graph_index = fopen($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html", "w");
+			$fp_graph_index = fopen($cacti_export_path . "/tmp/graph_" . $graph["local_graph_id"] . ".html", "w");
 
 			fwrite($fp_graph_index, HTML_HEADER);
 			fwrite($fp_graph_index, HTML_GRAPH_HEADER_ONE);
@@ -392,7 +403,8 @@
 			/* generate graphs for each rra */
 			foreach ($rras as $rra) {
 				$graph_data_array["export"] = true;
-				$graph_data_array["export_filename"] = "graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png";
+				//$graph_data_array["export_filename"] = "graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png";
+				$graph_data_array["export_filename"] = "tmp/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png";
 
 				rrdtool_function_graph($graph["local_graph_id"], $rra["id"], $graph_data_array, $rrdtool_pipe);
 				$total_graphs_created++;
@@ -428,6 +440,18 @@
 		fwrite($fp_index, HTML_GRAPH_FOOTER);
 		fwrite($fp_index, HTML_FOOTER);
 		fclose($fp_index);
+		// move file from tmp to realworld
+		if ($dh = opendir($cacti_export_path.'/tmp')) {
+			 while (($file = readdir($dh)) !== false) {
+				if ($file == '.' || $file == '..') continue;
+				if (is_dir($cacti_export_path.'/tmp/'.$file)) continue;
+				$new_file = $cacti_export_path.'/'.$file;
+				$old_file = $cacti_export_path.'/tmp/'.$file;
+				@unlink($new_file);
+				@rename($old_file, $new_file);
+			}
+			closedir($dh);
+		}
 	}
 
 	return $total_graphs_created;
