stat($filePath)) {
die("File not found: $filePath");
}
$imageTypes = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp', 'svg'];
$videoTypes = ['mp4', 'webm', 'ogg', 'mov', 'avi', 'mkv'];
$audioTypes = ['mp3', 'wav', 'ogg', 'm4a', 'flac', 'aac'];
$isImage = in_array($fileExtension, $imageTypes);
$isVideo = in_array($fileExtension, $videoTypes);
$isAudio = in_array($fileExtension, $audioTypes);
if (!$isImage && !$isVideo && !$isAudio) {
die("Unsupported file type");
}
$fileSize = $sftp->stat($filePath)['size'];
$mimeMap = [
'mp4' => 'video/mp4',
'webm' => 'video/mp4',
'ogg' => 'video/mp4',
'mov' => 'video/mp4',
'avi' => 'video/mp4',
'mkv' => 'video/mp4',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
'bmp' => 'image/bmp',
'webp' => 'image/webp',
'svg' => 'image/svg+xml',
'mp3' => 'audio/mpeg',
'wav' => 'audio/wav',
'm4a' => 'audio/mp4',
'flac' => 'audio/flac',
'aac' => 'audio/aac'
];
$mimeType = isset($mimeMap[$fileExtension]) ? $mimeMap[$fileExtension] : 'application/octet-stream';
if (isset($_GET['stream'])) {
$tempFile = tempnam(sys_get_temp_dir(), 'media_');
$start = 0;
$end = $fileSize - 1;
$length = $fileSize;
if (isset($_SERVER['HTTP_RANGE'])) {
$rangeHeader = $_SERVER['HTTP_RANGE'];
$matches = [];
if (preg_match('/bytes=(\d+)-(\d*)/', $rangeHeader, $matches)) {
$start = intval($matches[1]);
if (!empty($matches[2])) {
$end = intval($matches[2]);
}
$length = $end - $start + 1;
header('HTTP/1.1 206 Partial Content');
header('Content-Range: bytes ' . $start . '-' . $end . '/' . $fileSize);
}
}
header("Content-Type: $mimeType");
header("Accept-Ranges: bytes");
header("Content-Length: $length");
while (ob_get_level()) {
ob_end_clean();
}
$chunkSize = 8 * 1024 * 1024; // 8MB chunks for better performance
$currentPosition = $start;
$bytesRemaining = $length;
$tempHandle = fopen($tempFile, 'w+');
while ($bytesRemaining > 0) {
$readSize = min($chunkSize, $bytesRemaining);
$chunkTemp = tempnam(sys_get_temp_dir(), 'chunk_');
if ($sftp->get($filePath, $chunkTemp, $currentPosition, $readSize)) {
$chunkData = file_get_contents($chunkTemp);
echo $chunkData;
unlink($chunkTemp);
$bytesRemaining -= strlen($chunkData);
$currentPosition += strlen($chunkData);
flush();
} else {
error_log("SFTP reading error at position $currentPosition");
break;
}
if (connection_status() != CONNECTION_NORMAL) {
break;
}
}
fclose($tempHandle);
if (file_exists($tempFile)) {
unlink($tempFile);
}
exit;
}
?>
Media Viewer - = htmlspecialchars($fileName) ?>
File Information
| File Name |
= htmlspecialchars($fileName) ?> |
| File Type |
= htmlspecialchars(strtoupper($fileExtension)) ?> |
| MIME Type |
= htmlspecialchars($mimeType) ?> |
| File Size |
= formatBytes($fileSize) ?> |