-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.php
176 lines (147 loc) · 5.84 KB
/
files.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
$ad='accesskey';
if($_GET['delete']==$ad){setcookie("delete",$ad);$_COOKIE['delete']=$ad;}
function diff($t){
#This function calculates the difference between filetime and now, using D/H/M/S.
$s = time() - $t;
if($s<60){return 'Not long ago';}
elseif($s<3600){$z = (($s-($s%60))/60).'m';}
elseif($s<86400){$z = (($s-($s%3600))/3600).'h '.((($s-($s%60))/60)%60).'m ';}
elseif(1==1){$z = (($s-($s%86400))/86400)."d ".((($s-($s%3600))/3600)%24).'h '.((($s-($s%60))/60)%60).'m';}
return $z.' '.($s%60).'s ago';
}
// Function to fetch uploaded files from the database
function getUploadedFiles() {
$db = new SQLite3('uploads.db');
// Prepare SQL statement
$stmt = $db->prepare('SELECT * FROM uploads');
$result = $stmt->execute();
$files = [];
// Fetch uploaded files
while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
$files[] = $row;
}
// Close database connection
$db->close();
return $files;
}
// Function to create the SQLite database and table if they don't exist
function createDatabaseAndTableIfNeeded() {
$db = new SQLite3('uploads.db');
// Create uploads table if it doesn't exist
$db->exec('CREATE TABLE IF NOT EXISTS uploads (
id INTEGER PRIMARY KEY,
name TEXT,
type TEXT,
size INTEGER,
time INTEGER,
content BLOB
)');
// Close database connection
$db->close();
}
// Call function to create database and table if needed
if (!file_exists("uploads.db")){
createDatabaseAndTableIfNeeded();}
// Function to download a file
function downloadFile($fileId) {
// Database connection
$db = new SQLite3('uploads.db');
// Prepare SQL statement
$stmt = $db->prepare('SELECT * FROM uploads WHERE id = :id');
$stmt->bindValue(':id', $fileId, SQLITE3_INTEGER);
$result = $stmt->execute();
// Fetch file details
$file = $result->fetchArray(SQLITE3_ASSOC);
// Close database connection
$db->close();
// Set headers for file download
header('Content-Type: ' . $file['type']);
// header('Content-Disposition: attachment; filename="' . $file['name'] . '"');
header('Content-Length: ' . $file['size']);
// Output file content
echo $file['content'];
}
// Function to delete a file
function deleteFile($fileId) {
// Database connection
$db = new SQLite3('uploads.db');
// Prepare SQL statement
$stmt = $db->prepare('DELETE FROM uploads WHERE id = :id');
$stmt->bindValue(':id', $fileId, SQLITE3_INTEGER);
$result = $stmt->execute();
// Close database connection
$db->close();
return $result;
}
// Check if a file download request was made
if (isset($_GET['file_id'])) {
downloadFile($_GET['file_id']);
exit;
}
// Check if a file delete request was made
if (isset($_GET['delete_id'])) {
if($_COOKIE['delete']==$ad){deleteFile($_GET['delete_id']);}
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Database connection
$db = new SQLite3('uploads.db');
// Loop through each uploaded file, if uploaded
if (!empty($_FILES['files']['name'][0])) {
foreach ($_FILES['files']['name'] as $key => $fileName) {
// Get file details
$fileType = $_FILES['files']['type'][$key];
$fileTmpName = $_FILES['files']['tmp_name'][$key];
$fileError = $_FILES['files']['error'][$key];
$fileSize = $_FILES['files']['size'][$key];
// Read file contents
$fileContent = file_get_contents($fileTmpName);
// Prepare SQL statement
$stmt = $db->prepare('INSERT INTO uploads (name, type, size, time, content) VALUES (:name, :type, :size, :time, :content)');
$stmt->bindValue(':name', $fileName, SQLITE3_TEXT);
$stmt->bindValue(':type', $fileType, SQLITE3_TEXT);
$stmt->bindValue(':size', $fileSize, SQLITE3_INTEGER);
$stmt->bindValue(':time', time(), SQLITE3_INTEGER);
$stmt->bindValue(':content', $fileContent, SQLITE3_BLOB);
// Execute SQL statement
$result = $stmt->execute();
if (!$result) {
echo "Error uploading file: " . $fileName . "<br>";
}
}
echo "Files uploaded successfully!";}
// Close database connection
$db->close();
}
// Check if there are uploaded files to display
$uploadedFiles = getUploadedFiles();
?>
<!DOCTYPE html>
<html lang="en">
<style>
table,tr,td,input{border:2px solid #930;border-collapse:collapse;padding:0.3em}body{color:#930;font-family:Arial,sans-serif;margin:2%;padding:2%;background-color:#dfd}.container{max-width:600px;margin:50px auto;background-color:#fff;border-radius:10px;padding:20px;box-shadow:0 0 10px rgba(0,0,0,.1)}.container form{margin-bottom:20px}.container input[type="file"]{margin-bottom:10px}.container h2{margin-bottom:10px}.container ul{list-style:none;padding:0}.container ul li{margin-bottom:5px}
</style>
<body>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="files[]" id="files" multiple>
<input type="submit" value="Upload File" name="submit">
</form>
<h2>Uploaded Files:</h2>
<table>
<thead><td>Controls</td><td>Name</td><td>Size (B)</td><td>Upload time</td></thead>
<?php foreach ($uploadedFiles as $file): ?>
<tr>
<td><a href="?file_id=<?php echo $file['id']; ?>" target="_blank">⬇️</a>
<?php if($_COOKIE['delete']==$ad){echo'<a href="?delete_id='.$file['id'].'">❌</a></td>';} ?>
<td><?php echo $file['name']; ?></td>
<td><?php echo $file['size']; ?></td>
<td><span><?php echo diff($file['time']); ?></span></td>
</tr>
<?php endforeach; ?>
</table>
</body>
</html>