-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.php
executable file
·60 lines (50 loc) · 1.64 KB
/
image.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
<?php
session_start();
if (!isset($_SESSION['username'])) {
header("Location: login.php");
exit;
}
// Construct the full path to the file
if (!empty($requestedFile = $_GET['filename'])) {
$allowedDirectories = [
'/var/www/vunerable-web/img',
'img'
];
// Get the real path of the requested file
$requestedFile = realpath($requestedFile);
foreach ($allowedDirectories as $directory) {
// Check if the requested file is within the allowed directory
if (strpos($requestedFile, $directory) === 0) {
$filePath = $requestedFile;
break; // Exit the loop if a valid file path is found
}
}
if (file_exists($filePath)) {
$fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
// Set the appropriate content type header based on the file extension
switch ($fileExtension) {
case 'jpg':
case 'jpeg':
$contentType = 'image/jpeg';
break;
case 'png':
$contentType = 'image/png';
break;
case 'gif':
$contentType = 'image/gif';
break;
// Add more cases for other file types as needed
default:
$contentType = 'application/octet-stream';
break;
}
// Set the content type header
header('Content-Type: ' . $contentType);
// Read the file contents
$fileContents = file_get_contents($filePath);
// Output the file contents
echo $fileContents;
} else {
echo 'File not found.';
}
}