stream_get_contents
是 PHP 中一個用于從流中讀取數據的函數
$filename = 'example.txt';
$content = stream_get_contents($filename);
echo $content;
$url = 'https://example.com/file.txt';
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => 'User-Agent: Mozilla/5.0 (compatible; My_Example_Bot/1.0; +http://example.com/bot)'
]
]);
$content = stream_get_contents($url, false, $context);
echo $content;
$string = 'Hello, World!';
$content = stream_get_contents(fopen('data:text/plain;base64,' . base64_encode($string), 'r'));
echo $content;
$resource = fopen('php://memory', 'r');
fwrite($resource, 'Hello, World!');
rewind($resource);
$content = stream_get_contents($resource);
fclose($resource);
echo $content;
在這些示例中,stream_get_contents
函數用于從不同的數據源(文件、URL、字符串和資源)中讀取內容。這使得它在處理網絡編程和其他需要處理流數據的場景中非常有用。