<?php
session_start();
ob_start();
include_once("../dbconnect.php");

$action = isset($_GET['action']) ? $_GET['action'] : '';
 switch ($action) {
	 

	
    case 'add' :
        add();
        break;
    case 'delete' :
        delete();
        break;	
		
	    case 'edit' :
	     edit();
         break;
		
 
		
default :
        // if action is not defined or unknown
        // move to main category page
        header('Location: document.php');
		
}
function add()
{


if(($_FILES['image']['name']!=""))
{
	
$filetype = array('application/rtf','text/plain','text/csv','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/msword','application/vnd.openxmlformats-officedocument.presentationml.presentation','application/vnd.ms-powerpoint'
,'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel');
$ftype = false;

// check if uploaded file type is allowed

foreach($filetype as $type) {

	if ($type == $_FILES['image']['type']) {
	
		$ftype = true;
		break;
	
	}

}

   if($ftype)
   {
   
    $flext = pathinfo($_FILES['image']['name']);
 $ext = strtolower($flext['extension']);
 

   $qry=mysql_query("insert into upload(name,file,ext) values('".$_POST['name']."','".$_FILES['image']['name']."','".$ext."')");
   
   $lastid=mysql_insert_id();
  

   
   
echo   $target_path = "../document/".$lastid.".".$ext;
  
   move_uploaded_file($_FILES["image"]["tmp_name"],$target_path);
	$msg = $_FILES['image1']['name'].' uploaded successfully';
     }
    else
    {
	$msg="Acceptable file formats are: Ms Excel";
	}
}

else
{
	$msg="Please Upload File";
}







//header("location:document.php");
}



function delete()
{
 $sql = "DELETE FROM upload
			WHERE   u_id=".$_GET['id'];
			mysql_query($sql);
			
			header("location:document.php");


}
function edit()
{
if((isset($_POST['submit'])))
{
$filetype = array('application/rtf','text/plain','text/csv','application/vnd.openxmlformats-officedocument.wordprocessingml.document','application/msword','application/vnd.openxmlformats-officedocument.presentationml.presentation','application/vnd.ms-powerpoint'
,'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','application/vnd.ms-excel');
$ftype = false;

// check if uploaded file type is allowed

foreach($filetype as $type) {

	if ($type == $_FILES['image1']['type']) {
	
		$ftype = true;
		break;
	
	}

}

   if($ftype)
   {
    $flext = pathinfo($_FILES['image1']['name']);
 $ext = strtolower($flext['extension']);
 
   mysql_query("update upload  set name='".$_POST['name']."',file='".$_FILES['image1']['name']."', ext='".$ext."' where u_id=".$_POST['u_id']);
     echo  $lastid=$_POST['u_id'];

  $target_path = "../document/".$lastid.".".$ext;
   move_uploaded_file($_FILES["image1"]["tmp_name"],$target_path);
	$msg = $_FILES['image1']['name'].' uploaded successfully';
     }
    else
    {
	$msg="Acceptable file formats are: Ms Excel";
	}
}


header("location: document.php");
}

?>