Php/docs/function.stream-get-contents

提供:Dev Guides
< Php
移動先:案内検索

stream_get_contents

(PHP 5, PHP 7)

stream_get_contents残りのストリームを文字列に読み込む


説明

stream_get_contents ( resource $handle [, int $maxlength = -1 [, int $offset = -1 ]] ) : string|false

file_get_contents() と似ていますが、 stream_get_contents() は既にオープンしている ストリームリソースに対して操作を行います。そして、指定した offset から始まる最大 maxlength バイトのデータを取得して文字列に 保存します。


パラメータ

handle (resource)
ストリームリソース(例: fopen() の返す値)。
maxlength (int)
読み込む最大バイト数。デフォルトは -1 (バッファの残りのデータをすべて読み込む)。
offset (int)
読み込みを開始する前に移動する位置。負の数を指定した場合は移動が発生せず、 現在位置から読み込みを開始します。


返り値

文字列を返します。失敗した場合に false を返します。


例1 stream_get_contents() の例

<?phpif ($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() - ファイルポインタ上に残っているすべてのデータを出力する