PHP | stream_get_meta_data() Function

The stream_get_meta_data() function is an inbuilt function in PHP which is used to get the header or meta data from the streams/file pointers.
Syntax:
array stream_get_meta_data( $stream )
Parameters: The function accepts single parameter $stream, which specifies the meta data to be retrieve and which is created by any function fopen(), fsockopen() and pfsockopen().
Return Value: This function returns an array which contains the following items:
- timed_out: It is a boolean type items and TRUE if the stream timed out.
- blocked: It is a boolean type items and it is True if the stream is in blocking IO mode.
- eof(bool) It is optional. It is True if the stream reached at end-of-file.
- unread_bytes: Number of bytes in internal buffer.
- stream_type: It is used to specify the implementation of the stream.
- wrapper_type: It is used to specify the protocol wrapper implementation layer.
- wrapper_data: It is a specific data attached to this stream.
- mode: It is the type of access required for this stream.
- seekable: It is true when the current stream seeked.
- uri: User Provided Uniform resource identifier.
Below programs illustrate the stream_get_meta_data() function in PHP:
Program 1:
PHP
<?php// PHP program to illustrate// stream_get_meta_data function$file = fopen($url, 'r');$meta_data = stream_get_meta_data($file);print_r($meta_data);fclose($file);?> |
Output:
Array
(
[timed_out] =>
[blocked] => 1
[eof] =>
[wrapper_data] => Array
(
[0] => HTTP/1.1 200 OK
[1] => Server: nginx/1.10.3
[2] => Date: Mon, 17 Dec 2018 11:04:39 GMT
[3] => Content-Type: text/html; charset=utf-8
[4] => Connection: close
[5] => Content-language: en
[6] => X-Frame-Options: SAMEORIGIN
[7] => Set-Cookie: LAST_LANG=en; expires=Tue, 17-Dec-2019 11:04:39 GMT; Max-Age=31536000; path=/; domain=.php.net
[8] => Set-Cookie: COUNTRY=NA%2C54.201.119.186; expires=Mon, 24-Dec-2018 11:04:39 GMT; Max-Age=604800; path=/; domain=.php.net
[9] => Link: ; rel=shorturl
[10] => Last-Modified: Mon, 17 Dec 2018 05:06:18 GMT
[11] => Vary: Accept-Encoding
)
[wrapper_type] => http
[stream_type] => tcp_socket/ssl
[mode] => r
[unread_bytes] => 7647
[seekable] =>
[uri] => http://php.net/manual/en/function.stream-get-meta-data.php
)
Program 2: Program to print the length of array return by the function.
php
<?php// PHP program to illustrate // stream_get_meta_data function// url to be open using fopen// checking is url openable or notif (!$fp = fopen($url, 'r')) { trigger_error("Unable to open URL ($url)", E_USER_ERROR);}$fp = fopen($url, 'r');$meta_data = stream_get_meta_data($fp);print(sizeof($meta_data));fclose($fp);?> |
Output:
10
Reference: http://php.net/manual/en/function.stream-get-meta-data.php


