<?php
session_start();
include 'myfunctions.php';
	// require_once '../myfunctions.php';
$myVal="";

 
	//if(ISSET($_POST['upload'])){
		$image_name = $_FILES['image']['name'];
		$image_temp = $_FILES['image']['tmp_name'];
		$image_size = $_FILES['image']['size'];
		$ext = explode(".", $image_name);
		$end = end($ext);
		$allowed_ext = array("jpg", "jpeg", "gif", "png","doc","docx","xls","pdf","txt","mp4","mpeg","avi");
		$name = time().".".$end;
		if(!is_dir("upload/"))
		mkdir("upload/");
		$path = "upload/".$name;


	
	if(in_array($end, $allowed_ext)){
		//echo " ================ <br>" 
		//	. "image_name=" . $image_name . "<br>"
		//	. "image_temp=" . $image_temp . "<br>"
		//	. "image_size=" . $image_size . "<br> "
		//	. "path=" . $path ."<br>"
		//	. "name=" . $name;
			
			if($image_size > 5242880){
			//	echo "<script>alert('File too large!')</script>";
			//	echo "<script>window.location = 'index.php'</script>";
			}else{
			//	echo "<br>xxxxxxxxxxxxxxxx "   ;
				if(move_uploaded_file($image_temp, $path)){
			//		 echo "<br>xxxxxxxxxxxxxxxx 2 <br>" . $conn  ;
			//		 echo "INSERT INTO image (image, location) VALUES ('$name', '$path')";
					 
					mysqli_query($conn, "INSERT INTO image (image, location) VALUES ('$name', '$path')") or die(mysqli_error());
			//		echo "<br>xxxxxxxxxxxxxxxx 3"   ;
			//		echo "<script>alert('Image uploaded!')</script>";
					echo "<script>window.location = 'index.php'</script>";
				}
			}
		}else{
			echo "<script>alert('Invalid image format!')</script>";
			echo "<script>window.location = 'index.php'</script>";
		}
 
 
	//}
	
$myVal  = "image_name=$image_name\n";
$myVal .= "image_temp=$image_temp\n";
$myVal .= "image_size=$image_size\n";
$myVal .= "path=$path\n";
$myVal .= "name=$name";
 
echo  trim($myVal) ;
	
?>