-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.php
67 lines (63 loc) · 2.6 KB
/
file.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
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// save $_FILES['avatar'] to a inside a folder
$avatar = $_FILES['avatar'];
$avatar_name = $avatar['name'];
$avatar_tmp_name = $avatar['tmp_name'];
$avatar_size = $avatar['size'];
$avatar_error = $avatar['error'];
$avatar_ext = explode('.', $avatar_name);
$avatar_actual_ext = strtolower(end($avatar_ext));
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($avatar_actual_ext, $allowed)) {
if ($avatar_error === 0) {
if ($avatar_size < 1000000) {
$avatar_name_new = uniqid('', true) . '.' . $avatar_actual_ext;
$avatar_destination = 'upload/' . $avatar_name_new;
move_uploaded_file($avatar_tmp_name, $avatar_destination);
$_COOKIE['name'] = $_POST['name'];
$_COOKIE['email'] = $_POST['email'];
$_COOKIE['avatar'] = $avatar_destination;
setcookie('name', $_POST['name'], time() + 3600 * 24 * 7);
setcookie('email', $_POST['email'], time() + 3600 * 24 * 7);
setcookie('avatar', $avatar_destination, time() + 3600 * 24 * 7);
} else {
echo '<h1>File too big</h1>';
exit(1);
}
} else {
echo '<h1>Error</h1>';
exit(1);
}
} else {
echo '<h1>Not allowed</h1>';
exit(1);
}
} else if ($_SERVER['REQUEST_METHOD'] == 'GET' && isset($_GET['logout'])) {
unset($_COOKIE['name']);
unset($_COOKIE['email']);
unset($_COOKIE['avatar']);
setcookie('name', '', time() - 3600);
setcookie('email', '', time() - 3600);
setcookie('avatar', '', time() - 3600);
}
?>
<!DOCTYPE html>
<div>
<?php if (isset($_COOKIE['name']) && isset($_COOKIE['email'])): ?>
<p>
<center><h1>Hello, <?= $_COOKIE['name'] ?>!</h1></center><hr>
<center><img src="<?= $_COOKIE['avatar'] ?>" alt="avatar" width="300" height="300" style="object-fit: cover;"></center><hr><br/>
<center><?php echo $_COOKIE['name']; ?></center>
<center><?php echo $_COOKIE['email']; ?></center><br/><br/>
<center><a href="?logout">Logout</a></center>
</p>
<?php else: ?>
<form method="post" enctype="multipart/form-data">
<input type="text" name="name" placeholder="Name" />
<input type="text" name="email" placeholder="Email" />
<input type="file" id="avatar" name="avatar" accept="image/png, image/jpeg" />
<input type="submit" value="Submit" />
</form>
<?php endif; ?>
</div>