<?php
/**
 * This file uploads a file in the back end, without refreshing the page
 *  
 */
@session_start ();
$dirName = "uploads";

if (isset ( $_POST ['id'] )) {
	
	@mkdir ( $dirName, 0777 );
	
	$uploadFile = "$dirName/" . md5 ( $_FILES [$_POST ['id']] ['name'] . ".demo" );
	
	if (! is_dir ( $_GET ['dirname'] )) {
		echo '<script> alert("Failed to find the final upload directory: $dirName);</script>';
	}
	if (! copy ( $_FILES [$_POST ['id']] ['tmp_name'], $dirName . '/' . md5 ( $_FILES [$_POST ['id']] ['name'] . ".demo" ) )) {
		echo '<script> alert("Failed to upload file");</script>';
	}
} else {
	
	$uploadFile = "$dirName/" . md5 ( $_GET ['filename'] . ".demo" );
	if (file_exists ( $uploadFile )) {
		echo "File uploaded. <a href='$uploadFile'>Open File</a> &nbsp;&nbsp;&nbsp; <a href='deletefile.php?filename=" . $uploadFile . "'>Delete File</a>";
	} else {
		echo "<img src='loading.gif' alt='loading...' />";
	}
}
?>