<?php
define('YUKEY', true);
require(str_replace('upload','',dirname(__FILE__)) . '/includes/init.php');
	$error = "";
	$msg = "";
	$ext = array('jpg','gif','png','tif','pdf','doc','docx','xls','xlsx');
	$tbID=!empty($_GET['tbID']) ? intval($_GET['tbID']) : '';
	$type=!empty($_GET['type']) ? $_GET['type'] : '';
	$fileElementName = 'fileToUpload';
	if(!empty($_FILES[$fileElementName]['error']))
	{
		switch($_FILES[$fileElementName]['error'])
		{

			case '1':
				$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
				break;
			case '2':
				$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
				break;
			case '3':
				$error = 'The uploaded file was only partially uploaded';
				break;
			case '4':
				$error = 'No file was uploaded.';
				break;

			case '6':
				$error = 'Missing a temporary folder';
				break;
			case '7':
				$error = 'Failed to write file to disk';
				break;
			case '8':
				$error = 'File upload stopped by extension';
				break;
			case '999':
			default:
				$error = 'No error code avaiable';
		}
	}elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none')
	{
		$error = 'No file was uploaded..';
	}else 
	{
			//$msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
			//$msg .= " File Size: " . @filesize($_FILES['fileToUpload']['tmp_name']);
			//给图片改名字前必须得知该Defection的Id
			if($_FILES['fileToUpload']['size']>0 && $_FILES['fileToUpload']['size']<2000000){
			   $filename = basename($_FILES['fileToUpload']['name']);
	           $file_ext = substr($filename, strrpos($filename, '.') + 1);
				if(in_array($file_ext,$ext)){
				   if($type=='defect'){
					$usefile= $tbID.'.'.$file_ext;
					$sql = "UPDATE ". $GLOBALS['prefix']->table('find_defects'). " SET" . " tbPic='$usefile' WHERE tbFN='$tbID'";
					if($GLOBALS['db']->query($sql)){
					move_uploaded_file($_FILES['fileToUpload']['tmp_name'],'files/defects/'.$usefile);
					$msg = "Upload successful!";}
				   }elseif($type=='KitBom'){
				    $sql = "UPDATE ". $GLOBALS['prefix']->table('instructs'). " SET" . " tbKitBom ='$filename' WHERE tbWN='$tbID'";
					if($GLOBALS['db']->query($sql)){
					move_uploaded_file($_FILES['fileToUpload']['tmp_name'],'files/kit_bom/'.$filename);
					$msg = "Upload successful!";}
				   }elseif($type=='TestPlan'){
				    $sql = "UPDATE ". $GLOBALS['prefix']->table('instructs'). " SET" . " tbTestPlan ='$filename' WHERE tbWN='$tbID'";
					if($GLOBALS['db']->query($sql)){
					move_uploaded_file($_FILES['fileToUpload']['tmp_name'],'files/test_plan/'.$filename);
					$msg = "Upload successful!";}
				   }elseif($type=='Criteria'){
				    $sql = "UPDATE ". $GLOBALS['prefix']->table('instructs'). " SET" . " tbCriteria  ='$filename' WHERE tbWN='$tbID'";
					if($GLOBALS['db']->query($sql)){
					move_uploaded_file($_FILES['fileToUpload']['tmp_name'],'files/criteria/'.$filename);
					$msg = "Upload successful!";}
				   }elseif($type=='General'){
				    $sql = "UPDATE ". $GLOBALS['prefix']->table('instructs'). " SET" . " tbGeneral   ='$filename'";
					if($GLOBALS['db']->query($sql)){
					move_uploaded_file($_FILES['fileToUpload']['tmp_name'],'files/general/'.$filename);
					$msg = "Upload successful!";}
				   }
				}else{
				$msg = "Only jpg,gif,png,tif,pdf,doc,docx,xls,xlsx file allow to upload!";
				}
			}else{
			$msg = "Exceeded 2M file not allow to upload!";
			}
			//@unlink($_FILES['fileToUpload']);		
	}		
	echo "{";
	echo				"error: '" . $error . "',\n";
	echo				"msg: '" . $msg . "'\n";
	echo "}";
?>