-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchangeprofile-test.php
More file actions
94 lines (88 loc) · 2.98 KB
/
changeprofile-test.php
File metadata and controls
94 lines (88 loc) · 2.98 KB
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
<?php
require 'php/connection.php';
$_SESSION["id"] = 1; // User's session
$sessionId = $_SESSION["id"];
$user = mysqli_fetch_assoc(mysqli_query($con, "SELECT * FROM tb_user WHERE id = $sessionId"));
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Update Image Profile</title>
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://unicons.iconscout.com/release/v4.0.0/css/line.css">
</head>
<body>
<form class="form" id = "form" action="" enctype="multipart/form-data" method="post">
<div class="upload">
<?php
$id = $user["id"];
$name = $user["name"];
$image = $user["image"];
?>
<img src="img/<?php echo $image; ?>" width = 125 height = 125 title="<?php echo $image; ?>">
<div class="round">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="hidden" name="name" value="<?php echo $name; ?>">
<input type="file" name="image" id = "image" accept=".jpg, .jpeg, .png">
<label for="image"><i class = "uil uil-camera" style = "color: #000;" id="my-btn-camera"></i></label>
</div>
</div>
</form>
<script type="text/javascript">
document.getElementById("image").onchange = function(){
document.getElementById("form").submit();
};
</script>
<?php
if(isset($_FILES["image"]["name"])){
$id = $_POST["id"];
$name = $_POST["name"];
$imageName = $_FILES["image"]["name"];
$imageSize = $_FILES["image"]["size"];
$tmpName = $_FILES["image"]["tmp_name"];
// Image validation
$validImageExtension = ['jpg', 'jpeg', 'png'];
$imageExtension = explode('.', $imageName);
$imageExtension = strtolower(end($imageExtension));
if (!in_array($imageExtension, $validImageExtension)){
echo
"
<script>
alert('Invalid Image Extension');
document.location.href = './changeprofile-test.php';
</script>
";
}
elseif ($imageSize > 1200000){
echo
"
<script>
alert('Image Size Is Too Large');
document.location.href = './changeprofile-test.php';
</script>
";
}
else{
$newImageName = $name . " - " . date("Y.m.d") . " - " . date("h.i.sa"); // Generate new image name
$newImageName .= '.' . $imageExtension;
$query = "UPDATE tb_user SET image = '$newImageName' WHERE id = $id";
mysqli_query($con, $query);
move_uploaded_file($tmpName, 'img/' . $newImageName);
echo
"
<script>
document.location.href = './changeprofile-test.php';
</script>
";
}
}
?>
</body>
</html>
<script type="text/javascript" src="js/main.js"></script>
<script>
$('#my-btn-camera').click(function() {
})
</script>