Stream 函数
在线手册:中文  英文

stream_get_meta_data

(PHP 4 >= 4.3.0, PHP 5)

stream_get_meta_data从封装协议文件指针中取得报头/元数据

说明

array stream_get_meta_data ( int $fp )

返回现有 stream 的信息。可以是任何通过 fopen()fsockopen()pfsockopen() 建立的流。返回的数组包含以下项目:

以下项目是 PHP 4.3 新加的:

Note:

本函数是 PHP 4.3 引进的,在此版本之前,可以用 socket_get_status() 来取得前四个项目并且仅能用于基于 socket 的流

在 PHP 4.3 及以后版本中, socket_get_status() 是本函数的别名。

Note: 本函数不能作用于通过 Socket 扩展库创建的流。

以下项目为 PHP 5.0 新加:


Stream 函数
在线手册:中文  英文

用户评论:

niels at nise81 dot com (2008-03-03 05:49:07)

here is just an example how to read out all meta data.
how ever I found out that the "seekable"-entry doesn't exist in most of the streaming media files.
if (!($fp = @fopen($url, 'r')))
return NULL;
$meta = stream_get_meta_data($fp);

foreach(array_keys($meta) as $h){
$v = $meta[$h];
echo "".$h.": ".$v."<br/>";
if(is_array($v)){
foreach(array_keys($v) as $hh){
$vv = $v[$hh];
echo "_".$hh.": ".$vv."<br/>";
}
}
}
fclose($fp);

ed at readinged dot com (2003-01-28 19:54:46)

Below is a function I wrote to pull the "Last-Modified" header from a given URL. In PHP version 4.3 and above, it takes advantage of the stream_get_meta_data function, and in older version it uses a conventional GET procedure. On failure to connect to $url, it returns NULL. If the server does not return the Last-Modified header, it returns the current time. All times are returned in PHP's integer format (seconds since epoch).
Use it as so:
$last_modified = stream_last_modified('http://www.php.net/news.rss');
if (!is_null($last_modified))
if ($last_modified < time()-3600) //Older than an hour
echo 'URL is older than an hour.';
else
echo 'URL is fairly new.';
else
echo 'Invalid URL!';
function stream_last_modified($url)
{
if (function_exists('version_compare') && version_compare(phpversion(), '4.3.0') > 0)
{
if (!($fp = @fopen($url, 'r')))
return NULL;
$meta = stream_get_meta_data($fp);
for ($j = 0; isset($meta['wrapper_data'][$j]); $j++)
{
if (strstr(strtolower($meta['wrapper_data'][$j]), 'last-modified'))
{
$modtime = substr($meta['wrapper_data'][$j], 15);
break;
}
}
fclose($fp);
}
else
{
$parts = parse_url($url);
$host = $parts['host'];
$path = $parts['path'];
if (!($fp = @fsockopen($host, 80)))
return NULL;
$req = "HEAD $path HTTP/1.0\r\nUser-Agent: PHP/".phpversion()."\r\nHost: $host:80\r\nAccept: */*\r\n\r\n";
fputs($fp, $req);
while (!feof($fp))
{
$str = fgets($fp, 4096);
if (strstr(strtolower($str), 'last-modified'))
{
$modtime = substr($str, 15);
break;
}
}
fclose($fp);
}
return isset($modtime) ? strtotime($modtime) : time();
}

易百教程