

<?php
// Include the database configuration file
// include 'include/connection.php';

$dbHost     = "localhost";
$dbUsername = "u382534468_afsar";
$dbPassword = "Afsar@123";
$dbName     = "u382534468_afsar";
$rurl="https://afsarebrahim.com/admin/news/add-news.php";
// Create database connection
$db = new mysqli( $dbHost, $dbUsername, $dbPassword, $dbName );

// Check connection
if ( $db->connect_error ) {
    die( "Connection failed: " . $db->connect_error );
}

if ( isset( $_POST['add_img'] ) ) {

    // File upload configuration
    $targetDir = "newsgallery/";
    $status="active";

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

    $cat_id = $_POST['sel1'];

    $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';

    $fileNames = array_filter( $_FILES['files']['name'] );

    if ( !empty( $fileNames ) ) {

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

            // File upload path
            $fileName = basename( $_FILES['files']['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["files"]["tmp_name"][$key], $targetFilePath ) ) {

                    // Image db insert sql
                    $insertValuesSQL .= "('".$cat_id."','".$fileName."','".$status."' , NOW()),";

                } else {

                    $errorUpload .= $_FILES['files']['name'][$key].' | ';

                }

            } else {

                $errorUploadType .= $_FILES['files']['name'][$key].' | ';

            }

        }

        // 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 ) ) {

            $insertValuesSQL = trim( $insertValuesSQL, ',' );

            // Insert image file name into database
            
            $insert = $db->query( "INSERT INTO gallery (img_categ,img,status, uploaded_on) VALUES $insertValuesSQL" );

            if ( $insert ) {

                $statusMsg = "Files are uploaded successfully.";

                echo "<script> alert('$statusMsg')</script>";
                echo "<script>window.open('$rurl','_self')</script>";
            } else {

                $statusMsg = "Sorry, there was an error uploading your file.";

                echo "<script> alert(' $statusMsg')</script>";
                echo "<script>window.open('$rurl','_self')</script>";
            }

        } else {

            $statusMsg = "Upload failed! ".$errorMsg;

            echo "<script> alert(' $statusMsg')</script>";
            echo "<script>window.open('$rurl','_self')</script>";
        }

    } else {

        $statusMsg = 'Please select a file to upload.';

        echo "<script> alert('$statusMsg')</script>";
        echo "<script>window.open('$rurl','_self')</script>";
    }

}

?>