stream_get_contents 是 PHP 中的一个函数,用于从给定的流(比如文件、网络连接等)中读取数据,并将其作为字符串返回。以下是使用 stream_get_contents 的基本方法:
<?php$filename = 'example.txt';$content = stream_get_contents($filename);echo $content; // 输出文件内容?>从 URL 中读取内容:<?php$url = 'https://www.example.com/';$content = stream_get_contents($url);echo $content; // 输出 URL 内容?>从字符串中读取内容:<?php$string = "Hello, World!";$content = stream_get_contents($string);echo $content; // 输出字符串内容?>从资源中读取内容:<?php$resource = fopen('example.txt', 'r');$content = stream_get_contents($resource);echo $content; // 输出文件内容fclose($resource);?>注意:在使用 stream_get_contents 时,确保提供给它的流是有效的,否则它将返回 false。要检查流是否有效,可以使用 stream_is_valid() 函数。


