-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupload.php
87 lines (76 loc) · 2.59 KB
/
upload.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
<?php
// Connection Config
$conn= mysqli_connect("localhost","root","","practice");
if(!$conn){
header("location: https://httpstat.us/404");
}
if(isset($_POST['insert'])){
if($_FILES['image']['tmp_name']!=null){
$file=addslashes(file_get_contents($_FILES['image']['tmp_name']));
$sql= "INSERT INTO test values (null,'$file')";
if(mysqli_query($conn,$sql)){
echo '<script>alert("File Inserted successfully")</script>';
}else{
echo '<script>alert("Error in insertion")</script>';
}
}else{
echo '<script>alert("Please Select Image");</script>';
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
</head>
<body>
<div class="container" style="width: 500px">
<form method="POST" class="justify-content-center " enctype="multipart/form-data">
<h3 class="mt-4"> Insert Image</h3><br><br>
<input type="file" name="image" accept="image/*" required id="image">
<br><br>
<input type="submit" name="insert" id="insert" class="btn btn-primary" value="insert" onclick="selected_image();">
</form>
<br><br>
<table class="table table-bordered" width="60px">
<tr>
<th>Images from Database</th>
</tr>
<?php
$sql="SELECT * FROM `test`";
$result=mysqli_query($conn,$sql);
while ($row=mysqli_fetch_assoc($result)) {
echo '<tr>
<td>
<img src="data:image/jpeg;base64,'.base64_encode($row['Data']).'" style="width: 200px;">
</td>
</tr>';
}
?>
</table>
</div>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
function selected_image(){
var image_name = $('#image').val();
if(image_name == ''){
alert("please select Image");
return false;
}else{
var extension = $('#image').val();
var p = extension.lastIndexOf(".");
extension = extension.slice(p+1,extension.length).toLowerCase();
var ext = ["gif","png","jpeg","jpg"];
if(!ext.includes(extension)){
alert('unapropriate file selection Please select image file.');
$('#image').val('');
return false;
}
}
}
</script>
</body>
</html>