<?php
	//session_start();
	$name = $_FILES['file']['name'];
    $rand_num = mt_rand(1111,9999);
    $newName = date('y').date('m').date('d').$rand_num;
    $type = $_FILES['file']['type'];
    $allowed = array('application/pdf', 'image/png', 'image/jpeg', 'image/jpg');
    if (in_array($type, $allowed)) {
        $data = rawurlencode(file_get_contents($_FILES['file']['tmp_name']));

        $data_arr = array(
            "name" => $newName,
            "mime" => $type,
            "data" => $data
        );

        $datapayload = json_encode($data_arr);
		$url = 'http://172.31.11.56/hydro/module/common/insert_payment_doc.php';
        $api_request = curl_init($url);
       // $api_request = curl_init('http://hydrology.bwdb.gov.bd/includes/media_handler/upload/insert.php');
        curl_setopt($api_request, CURLOPT_URL, $url);
		
		curl_setopt($api_request, CURLOPT_VERBOSE, true);
        //curl_setopt($api_request, CURLOPT_URL, $url);
        curl_setopt($api_request, CURLOPT_POSTFIELDS, ["msg" => $datapayload]);
        curl_setopt($api_request, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($api_request, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($api_request, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($api_request);
		if (curl_errno($api_request)) {
			echo 'Curl error: ' . curl_error($api_request);
		}

        //  echo $result;die();
		//$show_result = $result;
        curl_close($api_request);

        if ($result) {
            $num = 1;
			$ret_msg = $result;
        } else {
           $ret_msg = 0;
        }
    } else {
        $ret_msg = "Only jpeg, jpg, png and pdf files are supported.";
    }
	echo $ret_msg;
?>