Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- session_start();
- require_once '../assets/php/db.conn.php';
- require_once '../assets/php/func.main.php';
- date_default_timezone_set('Asia/Manila');
- if(!isset($_SESSION)){
- header('Location: ../');
- }
- error_reporting(E_ALL);
- ini_set('display_errors', 1);
- $_targetdir = "../assets/images/";
- $imgErrors = array();
- if($_POST){
- $title = strip_tags(trim($_POST['title']));
- $content = strip_tags(trim($_POST['content']));
- $articleid = genRand();
- $datetime = date('Y-m-d H:i:s');
- $imgName = null;
- if(empty($title)){
- $msgs = '
- <div class="alert alert-warning alert-dismissable">
- <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
- <strong>Warning!</strong> Please add a title to your article!
- </div>
- ';
- } elseif(strlen($title) > 255){
- $msgs = '
- <div class="alert alert-warning alert-dismissable">
- <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
- <strong>Warning!</strong> Your title is too long! The maximum length is only 255 characters!
- </div>
- ';
- } elseif(empty($content)){
- $msgs = '
- <div class="alert alert-warning alert-dismissable">
- <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
- <strong>Warning!</strong> Please insert the content of your article!
- </div>
- ';
- }
- if(isset($_POST['imgUpload'])){
- $_targetfile = $_targetdir . basename($_FILES['imgUpload']["name"]);
- $filetype = pathinfo($_targetfile, PATHINFO_EXTENSION);
- $check = getimagesize($_FILES["imgUpload"]["tmp_name"]);
- if($check == false){
- array_push($imgErrors, 'File is not an image!');
- }
- if(file_exists($_targetfile)){
- array_push($imgErrors, 'Image already exists!');
- }
- if ($_FILES["imgUpload"]["size"] > 500000) {
- array_push($imgErrors, 'Image filesize is too big!');
- }
- if($filetype != "jpg" && $filetype != "png" && $filetype != "jpeg" && $filetype != "gif" ) {
- array_push($imgErrors, 'Sorry, your image file type is not supported!');
- }
- array_filter($imgErrors);
- if(empty($imgErrors)){
- if(move_uploaded_file($_FILES['imgUpload']['tmp_name'], $_targetfile)){
- $imgName = $_FILES["imgUpload"]["name"];
- }
- }
- }
- # var_dump() every possible variables, still getting the results I want
- var_dump($articleid);
- var_dump($title);
- var_dump($imgName);
- var_dump($content);
- var_dump($datetime);
- #$sql = "INSERT INTO `posts` (`articleid`, `title`, `image`, `content`, `created_at`) VALUES (?,?,?,?,?)";
- #$insertstmt = $conn->prepare($sql);
- #$insertstmt->execute(array($articleid, $title, $imgName, $content, $datetime));
- #var_dump($insertstmt);
- $insertsql = "INSERT INTO `posts` (`articleid`, `title`, `image`, `content`, `created_at`) VALUES (:AID, :TLT, :IMG, :CNT, :TM)";
- $inserstmt = $conn->prepare($insertsql);
- $inserstmt->bindParam(':AID', $articleid);
- $inserstmt->bindParam(':TLT',$title);
- $inserstmt->bindparam('IMG',$imgName);
- $inserstmt->bindParam(':CNT',$content);
- $inserstmt->bindParam(':TM', $timestamp);
- if($inserstmt){
- $msgs = '
- <div class="alert alert-success alert-dismissable">
- <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
- <strong>Success!</strong> Your article was posted successfully!
- </div>
- ';
- } else {
- $msgs = '
- <div class="alert alert-danger alert-dismissable">
- <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
- <strong>Error!</strong> The system encountered an error, please try again later!
- </div>
- ';
- }
- }
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
- <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
- <link rel="stylesheet" type="text/css" href="../assets/css/auth.css">
- <link rel="stylesheet" type="text/css" href="../assets/css/main.css">
- <link rel="stylesheet" type="text/css" href="../assets/css/imgUpload.css">
- <title>Dashboard | Stella Maris</title>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- <div class="navbar-header">
- <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
- <span class="sr-only">Toggle navigation</span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- <span class="icon-bar"></span>
- </button>
- <a class="navbar-brand" href="#">Stella Maris</a>
- </div>
- <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
- <ul class="nav navbar-nav">
- <li><a href="../"><span class="glyphicon glyphicon-home" aria-hidden="true"></span> Homepage</a></li>
- <li class="dropdown active">
- <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><span class="glyphicon glyphicon-signal" aria-hidden="true"></span> Dashboard <span class="caret"></span></a>
- <ul class="dropdown-menu" role="menu">
- <li><a href="index.php"><span class="glyphicon glyphicon-home" aria-hidden="true"></span> Home</a></li>
- <li class="divider"></li>
- <li><a href="#"><span class="glyphicon glyphicon-list-alt" aria-hidden="true"></span> New Post</a></li>
- </ul>
- </li>
- </ul>
- <ul class="nav navbar-nav navbar-right">
- <li class="dropdown">
- <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><span class="glyphicon glyphicon-user" aria-hidden="true"></span> Administrator <span class="caret"></span></a>
- <ul class="dropdown-menu" role="menu">
- <li><a href="../auth/logout.php"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span> Logout</a></li>
- </ul>
- </li>
- </ul>
- </div>
- </div>
- </nav>
- <div class="container">
- <form method="post" accept-charset="utf-8" enctype="multipart/form-data">
- <div class="form-group">
- <input type="text" name="title" placeholder="Your Article's Title" class="form-control" required maxlength="255">
- </div>
- <div class="form-group">
- <textarea name="content" placeholder="What's on your mind?" class="form-control" rows="5" required></textarea>
- </div>
- <div class="form-group">
- <div class="input-group">
- <span class="input-group-btn">
- <span class="btn btn-default btn-file">
- Browse… <input type="file" name="imgUpload" id="imgInp">
- </span>
- </span>
- <input type="text" class="form-control" readonly>
- </div>
- <div class="imgUpload text-center">
- <img id='img-upload'/>
- </div>
- </div>
- <input type="submit" name="submit" value="Post" class="btn btn-primary">
- <input type="reset" name="reset" value="Clear Fields" class="btn btn-default">
- </form>
- <?php
- if(isset($msgs)){
- echo '<hr>';
- echo $msgs;
- }
- if(!empty($imgErrors)){
- echo '<hr>';
- foreach($imgErrors as $err){
- echo $err;
- }
- }
- ?>
- <br><hr>
- <p>© Stella Maris</p>
- </div>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript" charset="utf-8" ></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/wow/1.1.2/wow.min.js" type="text/javascript" charset="utf-8"></script>
- <script src="../assets/js/imgUpload.js" type="text/javascript" charset="utf-8"></script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement