<?php

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

<?php

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

    // File upload configuration

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

        $idtrf = $_POST["idtrf"];

    }

    $targetDir = "uploadimages/";

    $allowTypes = array('jpg', 'png', 'jpeg', 'gif');



    $images_arr = array();

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

        $image_name = $_FILES['images']['name'][$key];

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

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

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

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



        // File upload path

        $code = time();

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

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

        $targetFilePath = $targetDir . $fileName;







        // Check whether file type is valid

        $fileType = strtolower(pathinfo($targetFilePath, PATHINFO_EXTENSION));

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

            // Store images on the server

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

                $images_arr[] = $targetFilePath;





                $UpdateQuery = new WA_MySQLi_Query($cmctrfdb);

                $UpdateQuery->Action = "update";

                $UpdateQuery->Table = "`trf-details`";

                $UpdateQuery->bindColumn("photofilename", "s", "$fileName", "WA_DEFAULT");

                $UpdateQuery->addFilter("idtrfdetails", "=", "i", "" . ($idtrf)  . "");

                $UpdateQuery->execute();

                $UpdateGoTo = "";



                $UpdateQuery->redirect($UpdateGoTo);

            }

        }

    }



    // Generate gallery view of the images

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

        <ul>

            <?php foreach ($images_arr as $image_src) { ?>

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

            <?php } ?>

        </ul>

<?php }

}

?>