-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy patheditPhoto.php
105 lines (94 loc) · 3.37 KB
/
editPhoto.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
<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect to login page
if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
header("location: login.php");
exit;
}
// Include config file
require_once "database.php";
// Define variables and initialize with empty values
$new_picture = $new_target = "";
$new_picture_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST")
{
// Validate new picture
if (!isset($_FILES['new_picture']))
{
$new_picture_err = "Image required";
}
else {
// Check for image file
$allowTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES['new_picture']['tmp_name']);
$error = !in_array($detectedType, $allowTypes);
if ($error) {
if (isset($_POST['Change'])) {
$new_picture_err = "File is not an image";
}
} // Check file size
elseif ($_FILES["new_picture"]["size"] > 8388608) {
if (isset($_POST['Change'])) {
$new_picture_err = "Sorry, your file is too large";
}
} else {
$new_picture = basename($_FILES['new_picture']['name']);
$new_target = "./images/" . $new_picture;
}
}
// Check input errors before updating the database
if(empty($new_picture_err)){
// Prepare an update statement
$sql = "UPDATE user SET picture = ? WHERE user_id = '". $_SESSION['id']."'";
if($stmt = $connection->prepare($sql)){
// Bind variables to the prepared statement as parameters
$stmt->bind_param("s", $param_new_picture);
// Set parameters
$param_new_picture = $new_picture;
// Attempt to execute the prepared statement
if($stmt->execute()){
// Photo updated successfully.
move_uploaded_file($_FILES["new_picture"]["tmp_name"],$new_target);
header("location:profile.php?id=". $_SESSION['id']);
} else{
echo "Oops! Something went wrong. Please try again later.";
}
// Close statement
$stmt->close();
}
}
// Close connection
$connection->close();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Change Photo</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
body{ font: 14px sans-serif; }
.wrapper{ width: 350px; padding: 20px; }
</style>
</head>
<body>
<div class="wrapper">
<h2>Change Photo</h2>
<p>Select a file to change the image.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group" <?php echo (!empty($new_picture_err)) ? 'has-error' : ''; ?>
<label>Profile picture</label>
<input type="file" title="Upload your profile picture" id="new_picture" name="new_picture">
<span class="help-block"><?php echo $new_picture_err;?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Change">
<a class="btn btn-link" href="profile.php">Cancel</a>
</div>
</form>
</div>
</body>
</html>