<?php
include('../helpers/init.php');

if(isset($_POST['addDownload'])){
	$error = "";
    $type = "Projects";
    $name = $_POST['name'];
	$image="dw_".rand(111,999)."_".basename($_FILES['file']['name']);
    
	if($_FILES['file']['size']>800000){
			header('Location:../pages/add-download.php?result=picfailer');
	}
    else{
		if (do_upload($image,"download")){
		$sql = "insert into download(type,name,document) values('$type','$name','$image')";
		if(query($sql)){
			header('Location:../pages/add-download.php?result=success');
		}else{
			$error = error();
			header('Location:../pages/add-download.php?result=failuer');
		}
		}
	}/*end else for size validation*/
	
	
}
//----------- Add Achievement Ends Here --------------


if(isset($_GET['action']) && $_GET['action']=="deleteDownload"){
	$id=$_GET['id'];
	$data = getWhere("download","id",$id);
    foreach($data as $row){
		$image= $row['document'];
        $path = "../uploads/download/";
        $delpic = $path.$image;
		$sql = "delete from download where id=$id";
        
		if(query($sql)){  
            unlink($delpic);
			header('Location:../pages/manage-download.php?result=success');
		}else{
			header('Location:../pages/manage-download.php?result=vfailure');
		}
	}
}

?>