<?php
// define a constant for the maximum upload size
include("../dbConnect.php");
  // define constant for upload folder
  define ('MAX_FILE_SIZE', 1024000);
  // replace any spaces in original filename with underscores
  // at the same time, assign to a simpler variable
  $file = str_replace(' ', '_', $_FILES['file']['name']);
  // convert the maximum size to KB
  $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
 
	define('UPLOAD_DIR', '../ads/');
  
  // create an array of permitted MIME types
  $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  // begin by assuming the file is unacceptable
  $sizeOK = false;
  $typeOK = false;
  
  // check that file is within the permitted size
  if ($_FILES['file']['size'] > 0 && $_FILES['file']['size'] <= MAX_FILE_SIZE) {
    $sizeOK = true;
	}	
  // check that file is of an permitted MIME type
  foreach ($permitted as $type) {
    if ($type == $_FILES['file']['type']) {
      $typeOK = true;
	  break;
	  }
	}
	
//Get the extension function	
  function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
	}
  //get the original name of the file from the clients machine
 		$filename = stripslashes($_FILES['file']['name']);
 	//get the extension of the file in a lower case format
  		$extension = getExtension($filename);
 		$extension = strtolower($extension);
		
  if ($sizeOK && $typeOK) {
    switch($_FILES['file']['error']) {
	  case 0:
		// $username would normally come from a session variable
      
		// if the user's subfolder doesn't exist yet, create it
	
        // check if a file of the same name has been uploaded
		
		  // move the file to the upload folder and rename it
		  $image_name=time().'.'.$extension;
		  $success = move_uploaded_file($_FILES['file']['tmp_name'], UPLOAD_DIR.$image_name);
		
		if ($success) {
          $result = "$file uploaded successfully";
          /*
		  if($_REQUEST['clientID']){
           $sql = "UPDATE `clients` SET `client_path`='$image_name' WHERE `client_id`= $id";
          }else if($_REQUEST['catID']){
           $sql = "UPDATE `gallery_cats` SET `cat_img`='$image_name' WHERE `cat_id`= $id";
          }else{
            
          } */
		  
		$date = date("Y-m-d H:i:s");
		  $sql = "INSERT into `Ads`  (`ID`, `image`, `date`, `title`) VALUES ('', '$image_name', '$date', ' ')";
		 
          $res=mysqli_query($hookup, $sql);
              if($res){
				header("Location: admin_extras.php");
              }
	      }
		else {
		  $result = "Error uploading $file. Please try again.";
		  }
	    break;
	  case 3:
		$result = "Error uploading $file. Please try again.";
	  default:
        $result = "System error uploading $file. Contact webmaster.";
	  }
    }
  elseif ($_FILES['file']['error'] == 4) {
    $result = 'No file selected';
	}
  else {
    $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png. <br/>Your image is ".$_FILES['file']['size'];
	}
      echo $typeOK."<br>";
      echo $sizeOK;
  echo $result;
?>
