<?php
	$dir = "../../asset/custom/upload/station/";
	//echo "file execute";
	
	$ret_msg = "";
	$file_name_holder = "";
	$filename = $_FILES['file']['name'];
	//echo $filename; exit;
	if($filename!='')
	{
		$file_new_name="";
		$validextensions = array("jpeg", "jpg", "JPG", "png", "PNG");
		$temporary = explode(".", $_FILES["file"]["name"]);
		$file_extension = end($temporary);
		if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg")) 
			&& in_array($file_extension, $validextensions)) {
			$fl = $dir . $filename;
			$fl = str_replace(' ', '_', $fl);
			$filename = str_replace(' ', '_', $filename);
			//echo $fl;
			if(file_exists($fl)) {
				//$ret_msg = "inside file exists if";
				$file_extension=explode('.',$filename);
				$index=count($file_extension)-1;
				$random_id = mt_rand(1,99);
				$file = explode(".",$filename);
				$file_name = $file[0].$random_id;
				//echo $file_name;
				//echo "W";
				$changed_file_name = $file_name;
				//echo $changed_file_name;
				$file_new_name=$changed_file_name.".".$file_extension[$index];
				$new_file_with_dir=$dir.$file_new_name;
				//echo $file_new_name;
				//rename($fl, $new_file_with_dir);
				move_uploaded_file($_FILES["file"]["tmp_name"], $new_file_with_dir);
				$file_name_holder = $new_file_with_dir;
			} else {
				move_uploaded_file($_FILES["file"]["tmp_name"], $fl);
				$file_name_holder = $fl;
			}
			$doc_location_arr = explode("../../",$file_name_holder);
			$doc_location = $doc_location_arr[1];
			$doc_url = $file_name_holder;
			$ret_msg = $doc_url;
			
			/*$output = putdoc($doc_url); 
			
			$outputArray = json_decode($output,true);
  
			if($outputArray == null)
			{
				//echo json_last_error() . "<br>";
				$ret_msg = $output; 
			}
			else
			{
				foreach ($outputArray as $value)
				{
					$ret_msg = $value['doc_source'];
					//echo "<br/>";
				}
			}*/
			//unlink($file_name_holder);
		}	
		else{
			$ret_msg = 0;
		}
	}
	echo $ret_msg;
	
	
	function putdoc($doc_url) 
	{
		$data_post =   array(
			"doc_url" => "$doc_url"
		);							
		$data_string = json_encode($data_post);
		$ch = curl_init('http://media.leotechbd.com/api/putdoc.php');                                                                    
		//$ch = curl_init('login_authentication.php');                                                                      
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
				'Content-Type: application/json',                                                                                
				'Content-Length: ' . strlen($data_string))                                                                       
		);                                                                                                                   
                                                                                                                     
		$response = curl_exec($ch);									
		curl_close($ch);
		return $response;
	}
?>
