-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsavePfp.php
More file actions
55 lines (35 loc) · 1.54 KB
/
savePfp.php
File metadata and controls
55 lines (35 loc) · 1.54 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
<?php
include("DBConnector.php");
if ($_SERVER["REQUEST_METHOD"] == "POST"){//Check if we're calling the php script with correct method
if (isset($_FILES["files"]["name"])){//Check if there's any actual file
//Name of the file
$fileToBeSaved = $_FILES["files"]["name"];
//Where we will save the files
$uploadLocation = "images_sent/";
//Complete future path of current file
$path = $uploadLocation.$fileToBeSaved;
$currUser = json_decode($_COOKIE["loggedIn"], true)["username"];
$query_getData = "SELECT * FROM users WHERE username = '$currUser'";
$result = mysqli_query($conn, $query_getData);
$userData = mysqli_fetch_assoc($result);
if (trim($userData["image"], " ") != "images/pfpDefault.jpg"){
try{
unlink(trim($userData["image"]));
}catch(mysqli_sql_exception $e){
}
}
if (move_uploaded_file($_FILES["files"]["tmp_name"], $path)){
$t = $dt = date("Y_m_d_h_i_sa");
$newName = "images_sent/pfp_". $userData["username"].trim($t);
rename($path, $newName);
echo $newName;
}else{
echo "Failed to upload file";
}
}else{
echo "There is no file to be uploaded";
}
}else{
echo "Invalid Request Method";
}
?>