File: /var/www/nclive/technc/add_exam_file.php
<?php include 'header.php'; ?>
<style>
.total_ques_div {
background-color: white;
border-color: #C8D5E0;
border-image: none;
border-style: solid;
border-width: 1px 1px 2px;
color: #1E598E;
font-weight: bold;
padding: 8px 10px;
text-decoration: none;
}
.form-table td {
padding: 0px !important;
}
.extLink {
text-decoration: none;
cursor: pointer;
}
.fancybox-skin {
margin-left: 400px !important;
width: 500px !important;
}
.form-table td {
padding: 5px !important;
font-size: 12px !important;
}
label.error {
color: #a94442;
font-weight: bold;
margin-top: 5px;
float: left;
font-size: 12px;
}
label {
display: inline-block;
max-width: 100%;
margin-bottom: 5px;
font-weight: 700;
}
</style>
<link href="assets/js/plugins/select2/select2.min.css" rel="stylesheet">
<script type="text/javascript" charset="utf8" src="assets/js/jquery-1.4.1.min.js"></script>
<script type="text/javascript" charset="utf8" src="assets/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="assets/css/jquery.dataTables.min.css">
<?php
// error_reporting(E_ALL);
// ini_set('display_errors', 'on');
function getExtension($str)
{
$i = strrpos($str, ".");
if (!$i) {
return "";
}
$l = strlen($str) - $i;
$ext = substr($str, $i + 1, $l);
return $ext;
}
if (isset($_POST['submit'])) {
$id = $_REQUEST['exam'];
$title = $_REQUEST['title'];
$tabname = '';
$status = $_REQUEST['status'];
$date = date('Y-m-d H:i:s');
$fileName = $_FILES['myfile']['name'];
$fileSize = $_FILES['myfile']['size'];
// $fileTmpName = $_FILES['myfile']['tmp_name'];
// $fileType = $_FILES['myfile']['type'];
$ext = strtolower(getExtension($fileName));
$target_dir = "/var/www/nclive/all_uploads/";
if ($ext != 'pdf') {
$message = "<font style='color:red; float:left;'><h5>Please choose PDF file <br>error on this file: $fileName</h5></font>";
} else if ($fileSize > 2097152) {
$message = "<font style='color:red; float:left;'><h5>This file is more than 2 MB. Sorry, it has to be less than or equal to 2 MB</h5></font>";
} else if ($fileSize == 0 || $fileSize == '0' || $fileSize == '') {
$message = "<font style='color:green; float:left;'><h5>This file is more than 2 MB. Sorry, it has to be less than or equal to 2 MB </h5></font>";
} else {
$extract_fname = pathinfo($fileName, PATHINFO_FILENAME);
$filter_fname = preg_replace('/[^a-zA-Z0-9\s]/', '', $extract_fname);
$new_fname = strtolower(str_replace(' ', '-', $filter_fname));
$new_fname = $new_fname . '.' . $ext;
if (!is_dir($target_dir)) {
mkdir($target_dir, 0777, true);
$target_file = $target_dir . $new_fname;
$didUpload = move_uploaded_file($_FILES['myfile']["tmp_name"], $target_file);
} else {
$target_file = $target_dir . $new_fname;
$didUpload = move_uploaded_file($_FILES['myfile']["tmp_name"], $target_file);
}
$insertquery = "INSERT INTO sp_exam_uploads SET exam_id='$id', title='$title', upload_file='$new_fname',showing_image='$tabname',tabname='$tabname',status='$status',add_by='$user_id',add_date='$date'";
$sql = $db->prepare($insertquery);
if ($sql->execute()) {
$message = "<font style='color:green; float:left;'><h5> Data Insert successfully</h5></font>";
} else {
$message = "<font style='color:red; float:left;'><h5> Data Not Inserted </h5></font>";
}
}
}
?>
<main id="main-container">
<div class="content content-narrow">
<div class="row">
<div class="col-lg-12">
<h2 class="content-heading"></h2>
<div class="block">
<div class="block-header">
<ul class="block-options">
<li>
<button type="button"><i class="si si-settings"></i></button>
</li>
</ul>
<h4 class="block-title"><?php if (isset($message)) {
echo $message;
} ?></h4>
</div>
<div class="block-content block-content-narrow">
<form class="js-validation-bootstrap form-horizontal" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="col-md-2 control-label" for="val-select2">Select Exam </label>
<div class="col-md-7">
<select class="js-select2 exam-dropdown form-control" name="exam" style="width: 100%;">
<?php $sql = $db->prepare("SELECT exam_id,exam_name FROM sp_exam ORDER by exam_name ASC");
$sql->execute(); ?>
<option>--Select Exam--</option>
<?php while ($row = $sql->fetch(PDO::FETCH_ASSOC)) { ?>
<option value="<?php echo $row['exam_id']; ?>"><?php echo $row['exam_name']; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="val-email">Title:<span class="text-danger">*</span></label>
<div class="col-md-7">
<input class="form-control" type="text" name="title" id="title" placeholder="" required>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="val-password">Upload File for Popup:<span class="text-danger">*</span></label>
<div class="col-md-7">
<input class="form-control" type="file" name="myfile" id="myfile" accept=".pdf" required>
<label class="error">Upload pdf file only upto 2 MB size:<span class="text-danger">*</span></label>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label" for="val-password">Status:<span class="text-danger">*</span></label>
<div class="col-md-7">
<select class="js-select2 form-control" name="status" style="width: 100%;">
<option value="1">Active</option>
<option value="0">Deactive</option>
</select>
</div>
</div>
<div class="form-group">
<div class="col-md-10 col-md-offset-2">
<input class="btn btn-lg btn-primary" name="submit" type="submit" value="Upload File">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="content content-narrow">
<div class="row">
<div class="col-lg-12">
<h2 class="content-heading">Upload List </h2>
<div class="table-responsive">
<table id="upload_list" class="table table-striped table-bordered dt-responsive nowrap" style="width: 100%; cellspacing: 0;">
<thead>
<tr class="danger">
<th>S.no</th>
<th>Exam Name</th>
<th>Title</th>
<th>status</th>
<th>Path</th>
</tr>
</thead>
</table>
<script type="text/javascript">
jQuery(function($) {
jQuery('#upload_list').dataTable({
"dom": "Bfrtip",
"pagingType": "full_numbers",
"processing": true,
"serverSide": true,
"responsive": true,
"ajax": "/technc/add_exam_file_data.php",
"columns": [{
"data": "id",
render: function(data, type, row, meta) {
return meta.row + meta.settings._iDisplayStart + 1;
}
},
{
"data": "exam_name"
},
{
"data": "title"
},
{
"data": "status"
},
{
"data": "file_path",
render: function(data, type, row, meta) {
var ret = '';
ret += '<a href="<?php echo ROOT_LINK; ?>' + row.file_path + '" target="_blank"><?php echo ROOT_LINK; ?>' + row.file_path + '</a>';
return ret;
}
}
],
"order": [
[0, 'desc']
],
"bPaginate": true,
"iDisplayLength": 50,
"bProcessing": true,
"bServerSide": true,
"lengthMenu": [
[50, 100, 200, 300, 1000],
[50, 100, 200, 300, "All"]
]
});
});
</script>
</div>
</div>
</div>
</div>
</main>
<?php include 'footer.php'; ?>
<script src="assets/js/plugins/select2/select2.min.js"></script>
<script>
$(document).ready(function() {
$('.exam-dropdown').select2();
});
$('#myfile').on('change', function() {
var numb = $(this)[0].files[0].size
if (numb > 2097152) {
// alert("Please upload file less than 2MB. Thanks!!");
$("#msg-error").css('display', 'block');
$("#msg-error").html('<div class="alert alert-danger"> <strong>File size exceeds 2 MB, Please choose image file less than 2 MB only </strong></div>');
$("#myfile").css('border', '#e66262 1px solid');
$('#myfile').focus();
$(this).val('');
} else {
$("#msg-error").css('display', 'none');
$("#myfile").css('border', '');
}
});
</script>