函数文档

wp_is_stream()

💡 云策文档标注

概述

wp_is_stream() 函数用于检测给定路径是否为流 URL。它通过检查路径中是否包含 '://' 分隔符和流协议是否在有效包装器列表中来实现。

关键要点

  • 参数:$path(字符串,必需),表示资源路径或 URL。
  • 返回值:布尔值,如果路径是流 URL 则返回 true,否则返回 false。
  • 核心逻辑:检查路径中是否存在 '://' 分隔符,并验证协议部分是否在 stream_get_wrappers() 返回的列表中。

代码示例

function wp_is_stream( $path ) {
    $scheme_separator = strpos( $path, '://' );

    if ( false === $scheme_separator ) {
        // $path isn't a stream.
        return false;
    }

    $stream = substr( $path, 0, $scheme_separator );

    return in_array( $stream, stream_get_wrappers(), true );
}

注意事项

  • 该函数自 WordPress 3.5.0 版本引入。
  • 常用于图像编辑器类(如 WP_Image_Editor_Imagick 和 WP_Image_Editor_GD)中处理文件或流操作。
  • 相关函数包括 path_is_absolute()、wp_normalize_path() 等,用于路径处理。

📄 原文内容

Tests if a given path is a stream URL

Parameters

$pathstringrequired
The resource path or URL.

Return

bool True if the path is a stream URL.

Source

function wp_is_stream( $path ) {
	$scheme_separator = strpos( $path, '://' );

	if ( false === $scheme_separator ) {
		// $path isn't a stream.
		return false;
	}

	$stream = substr( $path, 0, $scheme_separator );

	return in_array( $stream, stream_get_wrappers(), true );
}

Changelog

Version Description
3.5.0 Introduced.