Skip to content

Commit 7d9bac0

Browse files
authored
Add files via upload
1 parent 66366ad commit 7d9bac0

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed

css/style.css

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
body {
2+
font-family: Arial, sans-serif;
3+
background-color: #f5f5f5;
4+
text-align: center;
5+
margin: 0;
6+
padding: 0;
7+
}
8+
9+
h1 {
10+
color: #333;
11+
}
12+
13+
h2 {
14+
margin-top: 20px;
15+
color: #333;
16+
}
17+
18+
ul {
19+
list-style-type: none;
20+
padding: 0;
21+
margin: 0;
22+
}
23+
24+
li {
25+
margin: 5px 0;
26+
background-color: #fff;
27+
padding: 10px;
28+
border: 1px solid #ccc;
29+
border-radius: 5px;
30+
box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
31+
}
32+
33+
a {
34+
text-decoration: none;
35+
color: #007BFF;
36+
margin-right: 10px;
37+
font-weight: bold;
38+
}
39+
40+
a:hover {
41+
color: #0056b3;
42+
}
43+
44+
input[type="file"] {
45+
margin: 10px 0;
46+
}
47+
48+
input[type="text"] {
49+
padding: 10px;
50+
border: 1px solid #ccc;
51+
border-radius: 5px;
52+
}
53+
54+
input[type="submit"] {
55+
background-color: #007BFF;
56+
color: #fff;
57+
border: none;
58+
border-radius: 5px;
59+
padding: 10px 20px;
60+
font-size: 16px;
61+
cursor: pointer;
62+
}
63+
64+
input[type="submit"]:hover {
65+
background-color: #0056b3;
66+
}

delete.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
if (isset($_GET['file'])) {
3+
$file = 'uploads/' . $_GET['file'];
4+
5+
if (file_exists($file)) {
6+
unlink($file);
7+
header('Location: index.php');
8+
} else {
9+
echo "File not found.";
10+
}
11+
}
12+
?>

index.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>File Manager</title>
5+
<link rel="stylesheet" type="text/css" href="css/style.css">
6+
</head>
7+
<body>
8+
<h1>File Manager</h1>
9+
10+
<h2>File List</h2>
11+
<ul>
12+
<?php
13+
$files = scandir('uploads');
14+
foreach ($files as $file) {
15+
if ($file != "." && $file != "..") {
16+
$originalFileName = substr($file, 0, 40); // Display the first 40 characters of the original name
17+
18+
// Extract the user-defined display name from the file name
19+
$displayName = pathinfo($file, PATHINFO_FILENAME);
20+
21+
// Remove the hash part of the file name
22+
$displayName = preg_replace('/^[0-9a-f]{40}\./', '', $displayName);
23+
24+
// Display the display name as a link and the original file name as a download link
25+
echo "<li><a href='uploads/$file' target='_blank'>$displayName</a> <a href='uploads/$file' download>Download</a> <a href='delete.php?file=$file'>Delete</a></li>";
26+
}
27+
}
28+
?>
29+
</ul>
30+
31+
<h2>Upload a File</h2>
32+
<form action="upload.php" method="post" enctype="multipart/form-data">
33+
<input type="file" name="file" required>
34+
<input type="text" name="display_name" placeholder="Display Name">
35+
<input type="submit" value="Upload">
36+
</form>
37+
</body>
38+
</html>
39+

upload.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
if (isset($_FILES['file'])) {
3+
$uploadDir = 'uploads/';
4+
$originalFileName = $_FILES['file']['name'];
5+
$fileExtension = pathinfo($originalFileName, PATHINFO_EXTENSION);
6+
7+
// Get the display name from the form input (sanitize as needed)
8+
$displayName = isset($_POST['display_name']) ? $_POST['display_name'] : '';
9+
$displayName = trim($displayName);
10+
11+
if (empty($displayName)) {
12+
$displayName = pathinfo($originalFileName, PATHINFO_FILENAME);
13+
}
14+
15+
// Generate an obfuscated file name using a hash of the display name
16+
$hashedFileName = sha1($displayName) . ".$fileExtension";
17+
18+
$uploadFile = $uploadDir . $hashedFileName;
19+
20+
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) {
21+
header('Location: index.php');
22+
} else {
23+
echo "File upload failed.";
24+
}
25+
}
26+
?>
27+

0 commit comments

Comments
 (0)