-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteFile.php
More file actions
executable file
·58 lines (45 loc) · 1.55 KB
/
deleteFile.php
File metadata and controls
executable file
·58 lines (45 loc) · 1.55 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
<?php
$conn = require __DIR__ . "/database.php";
session_start();
if (!isset($_SESSION["email"])) {
// Redirect to the login page if the user is not logged in
header('Location: index.php');
exit();
}
$email = mysqli_real_escape_string($conn, $_SESSION['email']);
$query = "SELECT * FROM account WHERE email='$email'";
$result = mysqli_query($conn, $query);
if ($row = mysqli_fetch_assoc($result)) {
$name = $row['userName'];
$password = $row['password'];
$phone = $row['phoneNum'];
$role = $row['role'];
} else {
// Handle case where email is not found in the database
$name = '';
$password = '';
$phone = '';
$role = '';
}
$response = array('success' => false);
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["file_id"])) {
$fileId = $_POST["file_id"];
// Retrieve file information from the database
$getFileQuery = "SELECT * FROM files WHERE id = '$fileId'";
$fileResult = mysqli_query($conn, $getFileQuery);
if (mysqli_num_rows($fileResult) > 0) {
$file = mysqli_fetch_assoc($fileResult);
// Delete the physical file from the server
if (unlink($file['filename'])) {
// Delete the file record from the database
$deleteFileQuery = "DELETE FROM files WHERE id = '$fileId'";
$deleteResult = mysqli_query($conn, $deleteFileQuery);
if ($deleteResult) {
$response['success'] = true;
}
}
}
}
// Return the JSON response
header('Content-Type: application/json');
echo json_encode($response);