-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.php
More file actions
116 lines (95 loc) · 3.19 KB
/
view.php
File metadata and controls
116 lines (95 loc) · 3.19 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
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
<?php
// File viewer and downloader
// Serves media files securely
if (!isset($_GET['file'])) {
http_response_code(400);
die('No file specified');
}
// Decode file path (supports both base64 and regular paths)
$filePath = $_GET['file'];
if (isset($_GET['encoded']) && $_GET['encoded'] == '1') {
$filePath = base64_decode($filePath);
}
// Security check: Verify file exists and is readable
if (!file_exists($filePath) || !is_file($filePath) || !is_readable($filePath)) {
http_response_code(404);
// Debug output
header('Content-Type: text/plain');
echo "File not found or not accessible\n";
echo "Decoded path: " . $filePath . "\n";
echo "File exists: " . (file_exists($filePath) ? 'yes' : 'no') . "\n";
echo "Is file: " . (is_file($filePath) ? 'yes' : 'no') . "\n";
echo "Is readable: " . (is_readable($filePath) ? 'yes' : 'no') . "\n";
die();
}
// Get file information
$fileName = basename($filePath);
$fileExtension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
$fileSize = filesize($filePath);
// Define MIME types
$mimeTypes = [
// Images
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'webp' => 'image/webp',
'svg' => 'image/svg+xml',
// PDFs
'pdf' => 'application/pdf',
// Videos
'mp4' => 'video/mp4',
'webm' => 'video/webm',
'ogg' => 'video/ogg',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo'
];
// Get MIME type
$mimeType = isset($mimeTypes[$fileExtension]) ? $mimeTypes[$fileExtension] : 'application/octet-stream';
// Check if download is requested
$isDownload = isset($_GET['download']) && $_GET['download'] == '1';
// Clear output buffer
if (ob_get_level()) {
ob_end_clean();
}
// Set headers
header('Content-Type: ' . $mimeType);
header('Content-Length: ' . $fileSize);
header('Accept-Ranges: bytes');
if ($isDownload) {
header('Content-Disposition: attachment; filename="' . $fileName . '"');
} else {
header('Content-Disposition: inline; filename="' . $fileName . '"');
}
// Cache headers for better performance
header('Cache-Control: public, max-age=31536000');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + 31536000) . ' GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($filePath)) . ' GMT');
// Handle range requests for video streaming
if (isset($_SERVER['HTTP_RANGE'])) {
$range = $_SERVER['HTTP_RANGE'];
$range = str_replace('bytes=', '', $range);
$range = explode('-', $range);
$start = intval($range[0]);
$end = isset($range[1]) && $range[1] !== '' ? intval($range[1]) : $fileSize - 1;
$length = $end - $start + 1;
http_response_code(206);
header('Content-Range: bytes ' . $start . '-' . $end . '/' . $fileSize);
header('Content-Length: ' . $length);
$file = fopen($filePath, 'rb');
fseek($file, $start);
$buffer = 8192;
$bytesLeft = $length;
while ($bytesLeft > 0 && !feof($file)) {
$bytesToRead = min($buffer, $bytesLeft);
echo fread($file, $bytesToRead);
$bytesLeft -= $bytesToRead;
flush();
}
fclose($file);
} else {
// Regular file output
readfile($filePath);
}
exit;
?>