<?php
include "bank.php";
$result = $conn->query("SELECT * FROM `users` WHERE token = '".sanitize($_POST['token'])."' ");
$resn = $result->num_rows;
		 if ($resn > 0) {
				// output data of each row
				$row = $result->fetch_assoc();
				
		 

$valid_formats = array("jpg", "png", "gif", "jpeg", "JPG", "JPEG", "PNG", "GIF");

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

    if (strlen($name)) {
        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 < (4096 * 4096)) { // check it if it's bigger than 2 mb or no
                $imagename = md5(uniqid() . time()) . "." . $extension;
                $tmp = $_FILES['imagefile']['tmp_name'];
                if (move_uploaded_file($tmp, "madarek/" . $imagename)) {
        $upd = mysql_query("UPDATE $action SET img='uploads/" . $imagename . "' WHERE $do=$id");
                  echo "<img src='madarek/" . $imagename . "' width='100%' class='per20'>";
										$sql = "INSERT INTO madarek (uid, file, date) VALUES ('".$id."', 'madarek/" . $imagename . "', '".time()."')";

										if ($conn->query($sql) === TRUE) {
											
											echo "<img src='api/madarek/" . $imagename . "'  class='per20'>";
										} else {
											echo json_encode(array('status'=>0,'msg'=>'مشکل در ذخیره کردن در دیتا بیس'));
										}
					
		    // echo "UPDATE $action SET $img='http://www.anaseyr.com/uploads/" . $imagename . "' WHERE `id`='$hotelid'";
                } else {
				
				
                    echo "Could not move the file.";
                }
            } else {
                echo "اندازه تصویر شما خیلی بزرگ است";
            }
        } else {
            echo "فرمت غیر قابل قبول است";
        }
    } else {
        echo "لطفا تصویری انتخاب کنید";
    }
    
}
?>