<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.filesystem.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'ja',
  ),
  'this' => 
  array (
    0 => 'function.file-get-contents.php',
    1 => 'file_get_contents',
    2 => 'ファイルの内容を全て文字列に読み込む',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'ファイルシステム 関数',
  ),
  'prev' => 
  array (
    0 => 'function.file-exists.php',
    1 => 'file_exists',
  ),
  'next' => 
  array (
    0 => 'function.file-put-contents.php',
    1 => 'file_put_contents',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/filesystem/functions/file-get-contents.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.file-get-contents" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">file_get_contents</h1> 
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">file_get_contents</span> &mdash; <span class="dc-title">ファイルの内容を全て文字列に読み込む</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.file-get-contents-description">
  <h3 class="title">説明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>file_get_contents</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$filename</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$use_include_path</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.integer.php" class="type int">int</a></span></span> <code class="parameter">$length</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span><br>): <span class="type"><span class="type"><a href="language.types.string.php" class="type string">string</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment"> 
   この関数は <span class="function"><a href="function.file.php" class="function">file()</a></span> と似ていますが、
   <code class="parameter">offset</code> で指定した場所から開始し
   <code class="parameter">length</code> バイト分だけ
   ファイルの内容を文字列に読み込むという点が異なります。
   失敗した場合、<span class="function"><strong>file_get_contents()</strong></span> は <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> を返します。
  </p>
  <p class="para">
   <span class="function"><strong>file_get_contents()</strong></span>はファイルの内容を文字列に読み込む
   方法として好ましいものです。もしOSがサポートしていれば
   パフォーマンス向上のためにメモリマッピング技術が使用されます。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    空白のような特殊な文字を有する URI をオープンする場合には、
    <span class="function"><a href="function.urlencode.php" class="function">urlencode()</a></span> でその URI をエンコードする必要があります。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-get-contents-parameters">
  <h3 class="title">パラメータ</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       データを読み込みたいファイルの名前。
      </p>
     </dd>
    
    
     <dt><code class="parameter">use_include_path</code></dt>
     <dd>
      <blockquote class="note"><p><strong class="note">注意</strong>: 
       <p class="para">
        定数 <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong>
        を使用して <a href="ini.core.php#ini.include-path" class="link">インクルードパス</a>
        から探すことができます。
        この定数を使うことは、<a href="language.types.declarations.php#language.types.declarations.strict" class="link">強い型付け</a>
        が有効になっている場合は不可能です。なぜなら、
        <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong> が <span class="type"><a href="language.types.integer.php" class="type int">int</a></span> だからです。
        <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> を代わりに使いましょう。
       </p>
      </p></blockquote>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
      <p class="para">
       <span class="function"><a href="function.stream-context-create.php" class="function">stream_context_create()</a></span> で作成したコンテキストリソース。
       独自のコンテキストを使用する必要がない場合は、このパラメータに
       <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong> を指定します。
      </p>
     </dd>
    
    
     <dt><code class="parameter">offset</code></dt>
     <dd>
      <p class="para">
       元のストリーム上で、読み込みを開始するオフセット位置。
       負のオフセットは、ストリームの末尾からのオフセットと解釈されます。
      </p>
      <p class="para">
       リモートファイルに対するシーク (<code class="parameter">offset</code> 指定) はサポートしていません。
       オフセットが小さい場合はリモートファイルでのシークがうまくいくこともありますが、
       これはバッファリングされたストリーム上で動作しているだけのことです。
      </p>
     </dd>
    
    
     <dt><code class="parameter">length</code></dt>
     <dd>
      <p class="para">
       読み込むデータの最大バイト数。
       デフォルトは、ファイル終端に達するまで読み込みます。
       このパラメータは、フィルタが処理した後のストリームに適用されることに注意しましょう。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.file-get-contents-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   読み込んだデータを返します。失敗した場合に <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> を返します。
  </p>
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">この関数は論理値
<strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> を返す可能性がありますが、<strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> として評価される値を返す可能性もあります。
詳細については <a href="language.types.boolean.php" class="link">論理値</a>の
セクションを参照してください。この関数の返り値を調べるには
<a href="language.operators.comparison.php" class="link">===演算子</a> を
使用してください。</p></div>
 </div>


 <div class="refsect1 errors" id="refsect1-function.file-get-contents-errors">
  <h3 class="title">エラー / 例外</h3>
  <p class="para">
   <code class="parameter">filename</code> が見つからない場合、<code class="parameter">length</code>
   がゼロより小さい場合、あるいはストリーム内での指定した <code class="parameter">offset</code> へのシークが失敗した場合に
   <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> レベルのエラーが発生します。
  </p>
  <p class="para">
   <span class="function"><strong>file_get_contents()</strong></span> 関数がディレクトリに対して呼び出されると、
   Windows では <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> レベルのエラーが発生していました。
   PHP 7.4 以降では、Windows 以外のオペレーティングシステムでも同じ動きになっています。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.file-get-contents-changelog">
  <h3 class="title">変更履歴</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>バージョン</th>
       <th>説明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>8.0.0</td>
       <td>
        <code class="parameter">length</code> は、nullable になりました。
       </td>
      </tr>

      <tr>
       <td>7.1.0</td>
       <td>
        負の <code class="parameter">offset</code> をサポートするようになりました。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.file-get-contents-examples">
  <h3 class="title">例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>例1 とあるウェブサイトのホームページのソースの取得と出力</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$homepage </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$homepage</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-2">
    <p><strong>例2 include_path の検索</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// 厳密な型検査が有効な場合。つまり、declare(strict_types=1) の場合<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">// 厳密な型検査が有効でない場合<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILE_USE_INCLUDE_PATH</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
   <div class="example" id="example-3">
    <p><strong>例3 ファイルの一部の読み込み</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// 21 文字目から 14 文字ぶん読み込みます<br /></span><span style="color: #0000BB">$section </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'./people.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">20</span><span style="color: #007700">, </span><span style="color: #0000BB">14</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$section</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は、
たとえば以下のようになります。</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">string(14) &quot;lle Bjori Ro&quot;</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-4">
    <p><strong>例4 ストリームコンテキストの使用</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">// ストリームを作成します<br /></span><span style="color: #0000BB">$opts </span><span style="color: #007700">= [<br />  </span><span style="color: #DD0000">'http' </span><span style="color: #007700">=&gt; [<br />    </span><span style="color: #DD0000">'method' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"GET"</span><span style="color: #007700">,<br />    </span><span style="color: #DD0000">'header' </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"Accept-language: en\r\n" </span><span style="color: #007700">.<br />                </span><span style="color: #DD0000">"Cookie: foo=bar"</span><span style="color: #007700">,<br />  ]<br />];<br /><br /></span><span style="color: #0000BB">$context </span><span style="color: #007700">= </span><span style="color: #0000BB">stream_context_create</span><span style="color: #007700">(</span><span style="color: #0000BB">$opts</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// 上で設定した HTTP ヘッダを使用してファイルをオープンします<br /></span><span style="color: #0000BB">$file </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">, </span><span style="color: #0000BB">$context</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 notes" id="refsect1-function.file-get-contents-notes">
  <h3 class="title">注意</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">この関数はバイナリデータに対応しています。
</span></p></blockquote>
  <div class="tip"><strong class="tip">ヒント</strong><p class="simpara"><a href="filesystem.configuration.php#ini.allow-url-fopen" class="link">fopen wrappers</a> が有効の場合、この関数のファイル名として
URL を使用することができます。ファイル名の指定方法に関する詳細は
<span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> を参照ください。
<a href="wrappers.php" class="xref">サポートするプロトコル/ラッパー</a> には、さまざまなラッパーの機能やその使用法、
提供される定義済み変数などの情報がまとめられています。</p></div>
  <div class="warning"><strong class="warning">警告</strong><p class="para">IIS のような、いくつかの標準に
対応してない Web サーバーは、PHP に警告を発生させるような手順でデータを送信します。
このようなサーバーを使用する場合は、<a href="errorfunc.configuration.php#ini.error-reporting" class="link">
error_reporting</a> を警告を発生しないレベルまで小さくする必要があります。
PHP では、<code class="literal">https://</code> ラッパーでストリームをオープンする際に
バグがある IIS サーバーソフトウエアを検出することができ、この警告を抑制することができます。
あなたが <code class="literal">ssl://</code> ソケットを作成するために  <span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span> を使用している場合、
自らこの警告を検出し、抑制する必要があります。</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.file-get-contents-seealso">
  <h3 class="title">参考</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.file.php" class="function" rel="rdfs-seeAlso">file()</a> - ファイル全体を読み込んで配列に格納する</span></li>
    <li><span class="function"><a href="function.fgets.php" class="function" rel="rdfs-seeAlso">fgets()</a> - ファイルポインタから 1 行取得する</span></li>
    <li><span class="function"><a href="function.fread.php" class="function" rel="rdfs-seeAlso">fread()</a> - バイナリセーフなファイルの読み込み</span></li>
    <li><span class="function"><a href="function.readfile.php" class="function" rel="rdfs-seeAlso">readfile()</a> - ファイルを出力する</span></li>
    <li><span class="function"><a href="function.file-put-contents.php" class="function" rel="rdfs-seeAlso">file_put_contents()</a> - データをファイルに書き込む</span></li>
    <li><span class="function"><a href="function.stream-get-contents.php" class="function" rel="rdfs-seeAlso">stream_get_contents()</a> - 残りのストリームを文字列に読み込む</span></li>
    <li><span class="function"><a href="function.stream-context-create.php" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - ストリームコンテキストを作成する</span></li>
    <li><a href="reserved.variables.httpresponseheader.php" class="link">$http_response_header</a></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>