/** * @inheritdoc */ public function flush() { $this->header_output->set('Content-Type', $this->getContentType())->flush(); echo $this->get(self::CONTENT); }
/** * @inheritdoc */ public function flush() { $file_path = $this->get(self::FILE); if (!is_file($file_path)) { throw new NotFoundException("File '{$file_path}' does not exist."); } $file_size = filesize($file_path); $this->header_output->set('Content-Type', $this->getContentType()); $this->header_output->set('Content-Length', $file_size); $this->header_output->set('Content-Disposition', 'attachment; filename="' . basename($file_path) . '"'); $this->header_output->set('Accept-Ranges', 'bytes'); $range = $this->request_info->HTTP_RANGE; if (!$range) { $this->header_output->flush(); readfile($file_path); return; } list($name, $range) = explode('=', $range); $range = explode(',', $range)[0]; $range_arr = explode('-', $range); if (strtolower(trim($name)) !== 'bytes' or count($range_arr) !== 2) { throw new BadRequestException("Http header 'HTTP_RANGE' must be like 'bytes=xx-xxx'."); } if ($range_arr[0] === '') { $end = $file_size - 1; $start = $end - intval($range_arr[1]); } elseif ($range_arr[1] === '') { $end = $file_size - 1; $start = intval($range_arr[0]); } else { $end = intval($range_arr[1]); $start = intval($range_arr[0]); if (!$end) { $end = $file_size - 1; } } if ($start > $end) { throw new BadRequestException("'HTTP_RANGE: bytes=start-end', 'start' must be less than 'end'."); } $length = $end - $start + 1; if ($length >= $file_size) { $this->header_output->flush(); readfile($file_path); return; } $this->header_output->setHttpStatus(206); $this->header_output->set('Content-Range', "bytes {$start}-{$end}/{$file_size}"); $this->header_output->flush(); try { $fp = fopen($file_path, 'r'); if ($start) { fseek($fp, $start); } while ($length) { $read = $length > 8192 ? 8192 : $length; $length -= $read; print fread($fp, $read); } fclose($fp); } catch (Exception $e) { throw new ForbiddenException("Could not read the file '{$file_path}' !!!", 403, $e); } }