Open In App

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: 
 



Below programs illustrate the stream_get_meta_data() function in PHP:
Program 1: 
 




<?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 program to illustrate
// stream_get_meta_data function
 
// url to be open using fopen
 
// checking is url openable or not
if (!$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
 


Article Tags :