You are here

public function ResponseHeaderBag::makeDisposition in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/http-foundation/ResponseHeaderBag.php \Symfony\Component\HttpFoundation\ResponseHeaderBag::makeDisposition()

Generates a HTTP Content-Disposition field-value.

Parameters

string $disposition One of "inline" or "attachment":

string $filename A unicode string:

string $filenameFallback A string containing only ASCII characters that: is semantically equivalent to $filename. If the filename is already ASCII, it can be omitted, or just copied from $filename

Return value

string A string suitable for use as a Content-Disposition field-value.

Throws

\InvalidArgumentException

See also

RFC 6266

File

vendor/symfony/http-foundation/ResponseHeaderBag.php, line 239

Class

ResponseHeaderBag
ResponseHeaderBag is a container for Response HTTP headers.

Namespace

Symfony\Component\HttpFoundation

Code

public function makeDisposition($disposition, $filename, $filenameFallback = '') {
  if (!in_array($disposition, array(
    self::DISPOSITION_ATTACHMENT,
    self::DISPOSITION_INLINE,
  ))) {
    throw new \InvalidArgumentException(sprintf('The disposition must be either "%s" or "%s".', self::DISPOSITION_ATTACHMENT, self::DISPOSITION_INLINE));
  }
  if ('' == $filenameFallback) {
    $filenameFallback = $filename;
  }

  // filenameFallback is not ASCII.
  if (!preg_match('/^[\\x20-\\x7e]*$/', $filenameFallback)) {
    throw new \InvalidArgumentException('The filename fallback must only contain ASCII characters.');
  }

  // percent characters aren't safe in fallback.
  if (false !== strpos($filenameFallback, '%')) {
    throw new \InvalidArgumentException('The filename fallback cannot contain the "%" character.');
  }

  // path separators aren't allowed in either.
  if (false !== strpos($filename, '/') || false !== strpos($filename, '\\') || false !== strpos($filenameFallback, '/') || false !== strpos($filenameFallback, '\\')) {
    throw new \InvalidArgumentException('The filename and the fallback cannot contain the "/" and "\\" characters.');
  }
  $output = sprintf('%s; filename="%s"', $disposition, str_replace('"', '\\"', $filenameFallback));
  if ($filename !== $filenameFallback) {
    $output .= sprintf("; filename*=utf-8''%s", rawurlencode($filename));
  }
  return $output;
}