0 follower

Final Class Yiisoft\Http\ContentDispositionHeader

InheritanceYiisoft\Http\ContentDispositionHeader

Helps to build "Content-Disposition" header that complies to RFC-6266 and works in the majority of modern browsers.

See also https://tools.ietf.org/html/rfc6266.

Public Methods

Hide inherited methods

Method Description Defined By
attachment() Returns "Content-Disposition" header with "attachment" disposition. Yiisoft\Http\ContentDispositionHeader
inline() Returns "Content-Disposition" header with "inline" disposition. Yiisoft\Http\ContentDispositionHeader
name() Yiisoft\Http\ContentDispositionHeader
value() Returns Content-Disposition header value that is safe to use with both old and new browsers. Yiisoft\Http\ContentDispositionHeader

Constants

Hide inherited constants

Constant Value Description Defined By
ATTACHMENT 'attachment' Content sent with "attachment" disposition usually triggers download dialog. Yiisoft\Http\ContentDispositionHeader
INLINE 'inline' Content sent with "inline" disposition is usually displayed within the browser window. Yiisoft\Http\ContentDispositionHeader

Method Details

Hide inherited methods

attachment() public static method

Returns "Content-Disposition" header with "attachment" disposition.

See also value().

public static string attachment ( string|null $fileName null )
$fileName string|null

The file name.

                public static function attachment(?string $fileName = null): string
{
    return self::value(self::ATTACHMENT, $fileName);
}

            
inline() public static method

Returns "Content-Disposition" header with "inline" disposition.

See also value().

public static string inline ( string|null $fileName null )
$fileName string|null

The file name.

                public static function inline(?string $fileName = null): string
{
    return self::value(self::INLINE, $fileName);
}

            
name() public static method

public static string name ( )
return string

Content-Disposition header name.

                public static function name(): string
{
    return 'Content-Disposition';
}

            
value() public static method

Returns Content-Disposition header value that is safe to use with both old and new browsers.

Fallback name:

  • Causes issues if contains non-ASCII characters with codes less than 32 or more than 126.
  • Causes issues if contains urlencoded characters (starting with %) or % character. Some browsers interpret filename="X" as urlencoded name, some don't.
  • Causes issues if contains path separator characters such as \ or /.
  • Since value is wrapped with ", it should be escaped as \".
  • Since input could contain non-ASCII characters, fallback is obtained by transliteration.

UTF name:

  • Causes issues if contains path separator characters such as \ or /.
  • Should be urlencoded since headers are ASCII-only.
  • Could be omitted if it exactly matches fallback name.
public static string value ( string $type, string|null $fileName null )
$type string

The disposition type.

$fileName string|null

The file name.

throws InvalidArgumentException

if $type is incorrect.

                public static function value(string $type, ?string $fileName = null): string
{
    if (!in_array($type, [self::INLINE, self::ATTACHMENT])) {
        throw new InvalidArgumentException(
            'Disposition type must be either "' . self::ATTACHMENT . '" or "' . self::INLINE . '".'
        );
    }
    $header = $type;
    if ($fileName === null) {
        return $header;
    }
    $fileName = str_replace(['%', '/', '\\'], '_', $fileName);
    $fallbackName = FallbackNameCreator::create($fileName);
    $utfName = rawurlencode($fileName);
    $header .= "; filename=\"{$fallbackName}\"";
    if ($utfName !== $fallbackName) {
        $header .= "; filename*=utf-8''{$utfName}";
    }
    return $header;
}