<?php

 include('include/headscript.php'); ?>

<?php

if(!empty($_FILES['doc'])){

    // File upload configuration

	if (isset($_POST["idtrf"])) {

	$idtrf=$_POST["idtrf"]; }

	if (isset($_POST["idauditdpi"])) {

	$idauditdpi=$_POST["idauditdpi"]; }

	if (isset($_POST["dpicode"])) {

	$dpicode=$_POST["dpicode"]; }

	if (isset($_POST["filedescription"])) {

	$filedescription=$_POST["filedescription"]; }

	else { $filedescription=""; }

    $targetDir = "uploaddocuments/";

    $allowTypes = array('pdf','doc','xls','xlsx','docx','jpg');

    

    $docss_arr = array();

    foreach($_FILES['doc']['name'] as $key=>$val){

        $doc_name = $_FILES['doc']['name'][$key];

        $tmp_name   = $_FILES['doc']['tmp_name'][$key];

        $size       = $_FILES['doc']['size'][$key];

        $type       = $_FILES['doc']['type'][$key];

        $error      = $_FILES['doc']['error'][$key];

        

        // File upload path

		$code=time();

        $fileName = basename($_FILES['doc']['name'][$key]);

		$fileName=$dpicode.'-'.$idtrf.'-'.$code.$fileName;

        $targetFilePath = $targetDir . $fileName;

        

		

		

        // Check whether file type is valid

        $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

        if(in_array($fileType, $allowTypes)){    

            // Store images on the server

            if(move_uploaded_file($_FILES['doc']['tmp_name'][$key],$targetFilePath)){

                $docs_arr[] = $targetFilePath;

				

  $InsertQuery = new WA_MySQLi_Query($cmctrfdb);

  $InsertQuery->Action = "insert";

  $InsertQuery->Table = "fileattached";

  $InsertQuery->bindColumn("idtrfdetails", "i", "$idtrf", "WA_DEFAULT");

  $InsertQuery->bindColumn("filename_fileattached", "s", "$fileName", "WA_DEFAULT");

  $InsertQuery->bindColumn("description_fileattached", "s", "$filedescription", "WA_DEFAULT");

  $InsertQuery->bindColumn("idauditdpi", "i", "$idauditdpi", "WA_DEFAULT");

  $InsertQuery->saveInSession("");

  $InsertQuery->execute();

  $InsertGoTo = "";



  $InsertQuery->redirect($InsertGoTo);

            }

        }

    }

    

    // Generate gallery view of the images

    if(!empty($docss_arr)){ ?>

        <ul>

        <?php foreach($docs_arr as $doc_src){ ?>

            <img src="<?php echo $doc_src; ?>" alt="" width="200">

        <?php } ?>

        </ul>

    <?php }

}

?>



