<html>
<head><title>Store binary data into SQL Database</title></head>
<body>

<?php

// code that will be executed if the form has been submitted:

echo $_POST['submit'];

if(array_key_exists('submit',$_POST))
{
	echo "works";
	
    $username="crystal_write";
    $password="cweb785";
    $database="crystals";

    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
	echo "tmp name :    ".$tmpfilename = $_FILES['file_data']['tmp_name'];
	
	echo "filesize : ".$filesize = filesize($tmpfilename);
	
	echo "filetype ".$filetype = $_FILES['file_data']['type'];
    
	echo "filename:".$filename = $_FILES['file_data']['name'];
	echo "descript: ".$file_description = $_POST['file_description'];
	
	$data = addslashes(fread(fopen($tmpfilename, "r"), $filesize));
	
	$query="INSERT INTO images (description,image_data,filename,filesize,filetype) VALUES ('$file_description','$data','$filename','$filesize','$filetype')";

	//$query="INSERT INTO images (filename) VALUES ('$filename')";
	
    $result=mysql_query($query) or die(mysql_error());
    
    echo $result;

    $id= mysql_insert_id();
    
    echo "id: ".$id;
    
    mysql_close();
}

else{
// else show the form to submit new data:
?>
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF'] ; ?>" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="file_description" size="40"/>
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000"><br>
    File to upload/store in database:<br>
    <input type="file" name="file_data" size="40">
    <input type="submit" name="submit" value="submit">
    </form>

<?php

}

?>

</body>
</html>
