當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP stream_get_meta_data()用法及代碼示例

stream_get_meta_data()函數是PHP中的內置函數,用於從流/文件指針獲取標頭或元數據。

用法:

array stream_get_meta_data( $stream )

參數:該函數接受單個參數$stream,該參數指定要檢索的元數據,並由任何函數fopen(),fsockopen()和pfsockopen()創建。


返回值:此函數返回一個包含以下項的數組:

  • timed_out:這是布爾類型的項目,如果流超時,則為TRUE。
  • blocked:它是布爾類型的項目,如果流處於阻塞IO模式,則為True。
  • eof(bool):它是可選的。如果流到達文件末尾,則為True。
  • unread_bytes:內部緩衝區中的字節數。
  • stream_type:它用於指定流的實現。
  • wrapper_type:它用於指定協議包裝器實現層。
  • wrapper_data:它是附加到此流的特定數據。
  • mode: 這是此流所需的訪問類型。
  • seekable:當當前流尋找時,這是真的。
  • uri:用戶提供的格式錯誤的資源標識符。

以下示例程序旨在說明PHP中的stream_get_meta_data()函數:

程序1:

<?php 
  
// PHP program to illustrate 
// stream_get_meta_data function 
  
$url = 'http://php.net/manual/en/function.stream-get-meta-data.php'; 
  
$file = fopen($url, 'r'); 
$meta_data = stream_get_meta_data($file); 
  
print_r($meta_data); 
  
fclose($file); 
  
?>
輸出:
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
)

程序2:程序通過函數打印返回數組的長度。

<?php 
  
// PHP program to illustrate  
// stream_get_meta_data function 
  
// url to be open using fopen 
$url = 'http://www.php.net/news.rss'; 
  
// 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); 
?>
輸出:
10

參考: http://php.net/manual/en/function.stream-get-meta-data.php



相關用法


注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 PHP | stream_get_meta_data() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。