<?php
session_start();
ob_start();
 include "../config/config.php"; 
 include "../config/function.php"; 
 
 $private_key=get_privatekey($_SESSION['admin_name']);
 $request_from_server=$_SERVER['HTTP_REFERER'];
$this_server=$_SERVER['HTTP_HOST'];
if($request_from_server!=''){
	check_access_url($request_from_server,$this_server);
}

// echo $_SESSION['login_status'];echo "<br>";
// echo $_SESSION['private_key'];echo "<br>";
// echo $_SESSION['admin_name'];echo "<br>";

if($_SESSION['login_status']!='YES' || $_SESSION['private_key']!=$private_key || $_SESSION['private_key']=='' || $_SESSION['admin_name']=='')
{
    echo "Blank";
	header("Location:../AppLogin");
	exit();
}
else
{
    echo $_POST['submit'];
 if(isset($_POST['submit'])){ 
    // File upload configuration 
    echo $event_idn=RemoveSpecialChar($_POST['event_name']);
    echo $event_name=get_value("select event_name from tbl_event_master where event_idn='$event_idn'");
    $targetDir = "../../eventImage/"; 
    $allowTypes = array('jpg','png','jpeg','gif'); 
     
    $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = ''; 
    $fileNames = array_filter($_FILES['upload']['name']); 
    if(!empty($fileNames)){ 
        foreach($_FILES['upload']['name'] as $key=>$val){ 
            // File upload path 
            $fileName = basename($_FILES['upload']['name'][$key]); 
            $targetFilePath = $targetDir . $fileName; 
             
            // Check whether file type is valid 
            $fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION); 
            if(in_array($fileType, $allowTypes)){ 
                // Upload file to server 
                if(move_uploaded_file($_FILES["upload"]["tmp_name"][$key], $targetFilePath)){ 
                    // Image db insert sql 
                     echo "<br>".$SSql="INSERT INTO tbl_event_galary (eventImage, event_name,event_idn) VALUES ('$fileName','$event_name','$event_idn')";
                      mysqli_query($connection,$SSql);
                }else{ 
                    $errorUpload .= $_FILES['upload']['name'][$key].' | '; 
                } 
            }else{ 
                $errorUploadType .= $_FILES['upload']['name'][$key].' | '; 
            } 
        } 
         $statusMsg="";
        // // Error message 
        // $errorUpload = !empty($errorUpload)?'Upload Error: '.trim($errorUpload, ' | '):''; 
        // $errorUploadType = !empty($errorUploadType)?'File Type Error: '.trim($errorUploadType, ' | '):''; 
        // $errorMsg = !empty($errorUpload)?'<br/>'.$errorUpload.'<br/>'.$errorUploadType:'<br/>'.$errorUploadType; 
         
        // if(!empty($insertValuesSQL)){ 
        //   echo "<br>". $insertValuesSQL = trim($insertValuesSQL, '),('); 
        //     // Insert image file name into database 
        //     echo "<br>"."INSERT INTO images (eventImage, event_name,event_idn) VALUES $insertValuesSQL";
        //     $insert = mysqli_query($connection,"INSERT INTO images (eventImage, event_name,event_idn) VALUES $insertValuesSQL"); 
        //     if($insert){ 
        //         $statusMsg = "Files are uploaded successfully.".$errorMsg; 
        //     }else{ 
        //         $statusMsg = "Sorry, there was an error uploading your file."; 
        //     } 
        // }else{ 
        //     $statusMsg = "Upload failed! ".$errorMsg; 
        // } 
    }else{ 
        $statusMsg = 'Please select a file to upload.'; 
    } 
} 
 

} 
header("Location:../AddEventImage?msg=$statusMsg");
?>