<?php
ini_set('display_errors', 'On');
error_reporting(E_ERROR);

/*DEPRECATED Use version 2*/
/*The following code will upload the images after renaming them to the FuelBuddyImages folder*/

// array for JSON response (default to failure)
$response = array();
$response["success"] = 0;
$response["message"] = "Failed";

// check for required fields
if (isset($_POST['email']) && isset($_POST['img_name']) && isset($_POST['img_type']) && isset($_FILES['img_file'])) {
	$email = $_POST['email'];
	$img_name = $_POST['img_name'];
	$img_type = $_POST['img_type'];
	
	if(!empty($img_name)){
		//change picPath to only name and append email id
		$img_name = str_replace(" ", "_", $img_name);
		$img_name = $email . "." . basename($img_name);

		//set save path of file and change append email id to image name
		if(strcmp($img_type, "image") == 0){		
			$file_path = "../FuelBuddyImages/";
		}
		else{
			$file_path = "../FuelBuddyReceipts/";
		}	
		$file_path = $file_path . $img_name; 
					
		move_uploaded_file($_FILES['img_file']['tmp_name'], $file_path);
		
		$response["success"] = 1;
		$response["message"] = "Success";
				
		// echoing JSON response
		echo json_encode($response);
	}
	
	//else send generic failure message
	if($response["success"] == 0){
		$response["success"] = 0;
		$response["message"] = "Failed";
								 
		// echoing JSON response
		echo json_encode($response);
	}
}
else{
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";
 
    // echoing JSON response
    echo json_encode($response);
}

?>