<?php 
include "bank.php";
$result = $conn->query("SELECT * FROM `users` WHERE token = '".$_GET['token']."' ");
//echo "SELECT * FROM `users` WHERE token = '".$_GET['token']."' ";

		 if ($result->num_rows > 0) {
				// output data of each row
				$row = $result->fetch_assoc(); 
				$id = $row['id'];
				
					
					
	
	
					
					if(ISSET($_FILES)){
						$valid_formats = array("jpg", "png", "gif", "jpeg", "JPG", "JPEG", "PNG", "GIF");

						$name = $_FILES['image']['name']; 
						$size = $_FILES['image']['size'];

						
							list($sometext, $extension) = explode(".", $name); // get the file's extension 
							if (in_array($extension, $valid_formats)) { // check it if it's a valid format or not
								if ($size < (1096 * 1096)) { // check it if it's bigger than 2 mb or no
									$imagename = $id."u-". pdate("Y-n-d_H:i:s") . "." . $extension;
									$tmp = $_FILES['image']['tmp_name'];
									$goal_path = "madarek/".$imagename;
									if (move_uploaded_file($tmp, $goal_path)) {
										$sql = "INSERT INTO madarek (uid, file, date, mtype) VALUES ('".$id."', '".$goal_path."', '".time()."', '".sanitize($_POST['type'])."')";

										if ($conn->query($sql) === TRUE) {
											
											echo json_encode(array('status'=>1,'dir'=>$goal_path,'msg'=>'آپلود شد'));
										} else {
											echo json_encode(array('status'=>0,'msg'=>'مشکل در ذخیره کردن در دیتا بیس'));
										}
										
									} else {
										echo json_encode(array('status'=>0,'msg'=>'آپلود نشد'));  
									}
								} else {
									echo json_encode(array('status'=>0,'msg'=>'خطای سایز'));  
								}
							} else {
								echo json_encode(array('status'=>0,'msg'=>'خطای فرمت'));  
							}
					}else{
						echo json_encode(array('status'=>0,'msg'=>'لطفا دوباره سعی کنید')); 
					}
				
			} else {
				//echo "0 results";
				$data = array("userstats" => "0" ,"msg" => "token is invalide" );
				echo json_encode($data );
			}

	




?>