stream_get_contents
(PHP 5)
stream_get_contents — 残りのストリームを文字列に読み込む
説明
string stream_get_contents
( resource
$handle
[, int $maxlength = -1
[, int $offset = -1
]] )
file_get_contents() と似ていますが、
stream_get_contents() は既にオープンしている
ストリームリソースに対して操作を行います。そして、指定した
offset から始まる最大
maxlength バイトのデータを取得して文字列に
保存します。
パラメータ
返り値
文字列を返します。失敗した場合に FALSE を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 5.1.0 |
offset が追加されました。
|
例
例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);
}
?>
注意
注意: この関数はバイナリデータに対応しています。
参考
- fgets() - ファイルポインタから 1 行取得する
- fread() - バイナリセーフなファイルの読み込み
- fpassthru() - ファイルポインタ上に残っているすべてのデータを出力する