$size - 1) $end = $size - 1; $length = $end - $start + 1; $status = 206; } // Set headers if (function_exists('http_response_code')) { http_response_code($status); } else { header($_SERVER["SERVER_PROTOCOL"] . " " . $status); } header("Content-Type: video/mp4"); header("Accept-Ranges: bytes"); header("Content-Length: $length"); header('Content-Disposition: inline; filename="' . basename($remoteFile) . '"'); if ($status === 206) { header("Content-Range: bytes $start-$end/$size"); } // Open a temporary local stream for output buffering // Use ftp_nb_fget to fetch from FTP in chunks and output directly $tmpStream = fopen('php://output', 'wb'); if (!$tmpStream) { ftp_close($ftp); http_response_code(500); echo "Failed to open output stream."; exit; } // ftp_nb_fget doesn't support offset, so we have to download full or partial file manually // We'll download full file into php://output but skip bytes before $start by reading and discarding // Unfortunately, FTP protocol itself doesn't support partial retrieval starting at offset // so we have to download and skip bytes, or use ftp_raw to send REST command (some FTP servers support it) // We'll try REST command ftp_raw($ftp, "TYPE I"); // Switch to binary mode // Attempt REST command for offset $restResp = ftp_raw($ftp, "REST $start"); if (strpos(implode("\n", $restResp), '350') === false) { // REST not supported or failed - fallback to downloading entire file and skipping in PHP $start = 0; // ignore range start - will download entire file $length = $size; $status = 200; } $ret = ftp_nb_fget($ftp, $tmpStream, $remoteFile, FTP_BINARY); $bytesSent = 0; while ($ret == FTP_MOREDATA) { flush(); $ret = ftp_nb_continue($ftp); } fclose($tmpStream); ftp_close($ftp); } // If cached file does NOT exist, do caching download with progress page and play button if (!file_exists($cacheFile)) { // Show progress bar page with play button, video, speed and ETR display and direct play button echo '
FTP connection failed.
"; exit; } ftp_pasv($ftp, true); // Get file size for progress calculation $ftpSize = ftp_size($ftp, $relativePath); if ($ftpSize <= 0) { echo "Failed to get remote file size.