<?php
function fileNameExists($filename,$dir) {

    $i=1; $probeer=$filename;

    while(file_exists($dir.$probeer)) {
        $punt=strrpos($filename,".");
        if(substr($filename,($punt-3),1)!==("[") && substr($filename,($punt-1),1)!==("]")) {
            $probeer=substr($filename,0,$punt)."[".$i."]".
            substr($filename,($punt),strlen($filename)-$punt);
        } else {
            $probeer=substr($filename,0,($punt-3))."[".$i."]".
            substr($filename,($punt),strlen($filename)-$punt);
        }
        $i++;
    }
    return $probeer;
}

include_once("func.php");

//path to storage
$storage = '../BussinessCard/images/thumbnail';
$newFolder=$_REQUEST["folderName"];
//mkdir($storage."/".$newFolder, 0777);
$finalStorage=$storage;//."/".$newFolder;
//exec("chmod 777 {$finalStorage} -Rf");
$file_name = fileNameExists($_FILES['Filedata']['name'],$storage."/");
//path name of file for storage
$uploadfile = "$finalStorage/" .  $file_name;



$username = $_REQUEST['username'];
//if the file is moved successfully
 //move_uploaded_file( $_FILES['Filedata']['tmp_name'] , $uploadfile );
if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadfile))
{
	//$conn = @mysql_connect($db_server, $db_username, $db_password);
	//$db_link = mysql_select_db($db_name,$conn);
	
//	$db = mysql_connect("localhost", "root", "") or die(mysql_error());
//mysql_select_db("ndc360_designs",$db) or die(mysql_error());

	
	$insert_sql  = "insert into imageinfo (UserId, ImagePath, TimeCreated) values('".$username."','".$file_name."',now())";
	$insert_query = mysql_query($insert_sql, $db);

	$answer="ok";
	echo "answer=".$answer;
}
else
{
	$answer="nope";
	echo "answer=".$answer;
}

?>