How to access fields inside a stream resource in PHP?

Viewed 268

I cannot access individual fields inside a stream resource (called $handle).

The resource is a result of me checking if an image file exists on the server:

$handle = @fopen($cdnPath . "/adblock" . $site_id . '.jpg', 'r');

If it does, I get this when id execute dump($handle);

stream resource @97 ▼
  crypto: array:4 [▶]
  timed_out: false
  blocked: true
  eof: false
  wrapper_data: array:17 [▶]
  wrapper_type: "http"
  stream_type: "tcp_socket/ssl"
  mode: "r"
  unread_bytes: 552
  seekable: false
  uri: "https://cdn.pixfuture.com/adblock_logo/adblock14.jpg"
  options: []
}

How can I access the uri field, and store it into a variable? I tried $handle->uri, and $handle['uri'], none of which worked.

1 Answers

You use stream_get_meta_data. Docs.


$f    = fopen($uri, 'r');
$meta = stream_get_meta_data($f);

echo $meta['uri'];
Related