stream_get_contents
(PHP 5, PHP 7, PHP 8)
stream_get_contents — 讀取串流的剩餘部分到字串中
參數
stream
(resource)
-
一個串流資源(例如從 fopen() 返回的)
length
(int)
-
要讀取的最大位元組數。預設為 null
(讀取所有剩餘的緩衝區)。
offset
(int)
-
在讀取之前,搜尋到指定的偏移量。如果此數字為負數,則不會進行搜尋,讀取將從目前位置開始。
範例
範例 #1 stream_get_contents() 範例
<?php
if ($stream = fopen('http://www.example.com', 'r')) {
// 從偏移量 10 開始印出整個頁面
echo stream_get_contents($stream, -1, 10);
fclose($stream);
}
if ($stream = fopen('http://www.example.net', 'r')) {
// 印出前 5 個位元組
echo stream_get_contents($stream, 5);
fclose($stream);
}
?>
注意事項
注意:此函式是二進位安全的。
注意:
當指定 length
值而不是 null
時,即使實際內容明顯較短,此函式也會立即分配該大小的內部緩衝區。