-
Notifications
You must be signed in to change notification settings - Fork 0
/
task6.php
115 lines (104 loc) · 3.92 KB
/
task6.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
require './helpers/validate.php';
require './dbcon.php';
error_reporting(0);
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$title = clean($_POST['title']);
$content = clean($_POST['content']);
$img_file = $_FILES['image']['name'];
$error_msg = [];
#title validation
if (!validate($title, 1)) {
$error_msg['title'] = 'This Field is required';
}
#content validation
if (!validate($content, 1)) {
$error_msg['content'] = 'This Field is required';
} elseif (!validate($content, 3, 50)) {
$error_msg['content'] = 'Article content must be more than 50 chars';
}
if (!empty($_FILES['image']['name'])) {
$imgTmp = $_FILES['image']['tmp_name'];
$imgName = $_FILES['image']['name'];
$imgType = $_FILES['image']['type'];
$imgSize = $_FILES['image']['size'];
$imgError = $_FILES['image']['error'];
$allowdEx = ['jpg', 'png', 'jpeg'];
$ExeArray = explode('.', $imgName);
if (in_array($ExeArray[1], $allowdEx)) {
$newName = uniqid('img_file', False) . '.' . $ExeArray[1];
$dirPath = './uploads/' . $newName;
if (move_uploaded_file($imgTmp, $dirPath)) {
echo '<div class="alert alert-success" role="alert" style="text-align: center;">
Image Uploaded successfully . <br>
</div>';
} else {
echo '<div class="alert alert-danger" role="alert" style="text-align: center;">
Error while uploading file try again. "<br>"</div>';
}
} else {
echo '<div class="alert alert-danger" role="alert" style="text-align: center;">
Not Allowed Extension . "<br>"</div>';
}
} else {
echo '<div class="alert alert-danger" role="alert" style="text-align: center;">
* => Image : Please upload an image <br>
</div>';
}
if (count($error_msg) > 0) {
foreach ($error_msg as $key => $val) {
echo "<div class='alert alert-danger w-75 p-3' role='alert' style='text-align: center;'>
* => $key : $val <br>
</div>";
}
}
else {
$query = "INSERT INTO `articles` (`title`,`content`,`image`) VALUES ('$title','$content','$imgName')";
$sql_exe = mysqli_query($conn,$query);
if($sql_exe){
echo '<div class="alert alert-success" role="alert" style="text-align: center;">
Data inserted into DB successfully. <br></div>';
}else {
echo '<div class="alert alert-danger" role="alert" style="text-align: center;">
Error unsaved Data . "<br>"</div>';
}
mysqli_close($conn);
}
}
?>
<?php include './layout/header.php'?>
<body>
<div class="container">
<h1>Article Form</h1>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-25">
<label for="name"><b>Title : </b></label>
</div>
<div class="col-75">
<input type="text" id="title" name="title" placeholder="Your Article Title..">
</div>
</div>
<div class="row">
<div class="col-25">
<label for="email"><b>Content : </b></label>
</div>
<div class="col-75">
<textarea rows="7" cols="75" id="content" name="content" class="content" placeholder="Your Content Here.."></textarea>
</div>
</div>
<div class="row">
<div class="col-25">
<label for="country"><b>Image : </b></label>
</div>
<div class="col-75">
<input type="file" id="image" name="image" class="image" placeholder="Your Password..">
</div>
</div>
<div class="submit">
<input type="submit" value="Submit">
</div>
</form>
</div>
</body>
</html>