Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 3060

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Reads entire file into a string</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.file-exists.html">file_exists</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.file-put-contents.html">file_put_contents</a></div>
 <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><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)</p><p class="refpurpose"><span class="refname">file_get_contents</span> &mdash; <span class="dc-title">Reads entire file into a string</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.file-get-contents-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">string</span> <span class="methodname"><strong>file_get_contents</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span>
   [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$use_include_path</code><span class="initializer"> = false</span></span>
   [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = -1</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$maxlen</code></span>
  ]]]] )</div>

  <p class="para rdfs-comment"> 
   This function is similar to  <span class="function"><a href="function.file.html" class="function">file()</a></span>, except that
    <span class="function"><strong>file_get_contents()</strong></span> returns the file in a 
   <span class="type"><a href="language.types.string.html" class="type string">string</a></span>, starting at the specified <em><code class="parameter">offset</code></em> 
   up to <em><code class="parameter">maxlen</code></em> bytes. On failure, 
    <span class="function"><strong>file_get_contents()</strong></span> will return <strong><code>FALSE</code></strong>.
  </p>
  <p class="para">
    <span class="function"><strong>file_get_contents()</strong></span> is the preferred way to read the
   contents of a file into a string.  It will use memory mapping techniques if
   supported by your OS to enhance performance.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    If you&#039;re opening a URI with special characters, such as spaces, you
    need to encode the URI with  <span class="function"><a href="function.urlencode.html" class="function">urlencode()</a></span>.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-get-contents-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    <dt>

     <span class="term"><em><code class="parameter">filename</code></em></span>
     <dd>

      <p class="para">
       Name of the file to read.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">use_include_path</code></em></span>
     <dd>

      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <p class="para">
        As of PHP 5 the <strong><code>FILE_USE_INCLUDE_PATH</code></strong> constant can be used
        to trigger <a href="ini.core.html#ini.include-path" class="link">include path</a>
        search.
       </p>
      </p></blockquote>
      
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">context</code></em></span>
     <dd>

      <p class="para">
       A valid context resource created with 
        <span class="function"><a href="function.stream-context-create.html" class="function">stream_context_create()</a></span>. If you don&#039;t need to use a
       custom context, you can skip this parameter by <strong><code>NULL</code></strong>.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">offset</code></em></span>
     <dd>

      <p class="para">
       The offset where the reading starts on the original stream.
      </p>
      <p class="para">
       Seeking (<em><code class="parameter">offset</code></em>) is not supported with remote files.
       Attempting to seek on non-local files may work with small offsets, but this
       is unpredictable because it works on the buffered stream.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">maxlen</code></em></span>
     <dd>

      <p class="para">
       Maximum length of data read. The default is to read until end
       of file is reached. Note that this parameter is applied to the 
       stream processed by the filters.
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.file-get-contents-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   The function returns the read data  or <strong><code>FALSE</code></strong> on failure.
  </p>
  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may
return Boolean <strong><code>FALSE</code></strong>, but may also return a non-Boolean value which
evaluates to <strong><code>FALSE</code></strong>. Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for more
information. Use <a href="language.operators.comparison.html" class="link">the ===
operator</a> for testing the return value of this
function.</p></div>
 </div>


 <div class="refsect1 errors" id="refsect1-function.file-get-contents-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
   An <strong><code>E_WARNING</code></strong> level error is generated if <em><code class="parameter">filename</code></em> cannot be found, <em><code class="parameter">maxlength</code></em>
   is less than zero, or if seeking to the specified <em><code class="parameter">offset</code></em> in the stream fails.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.file-get-contents-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-2365">
    <p><strong>Example #1 Get and output the source of the homepage of a website</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$homepage&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;</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-2366">
    <p><strong>Example #2 Searching within the 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">//&nbsp;&lt;=&nbsp;PHP&nbsp;5<br /></span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;&gt;&nbsp;PHP&nbsp;5<br /></span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</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-2367">
    <p><strong>Example #3 Reading a section of a file</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">//&nbsp;Read&nbsp;14&nbsp;characters&nbsp;starting&nbsp;from&nbsp;the&nbsp;21st&nbsp;character<br /></span><span style="color: #0000BB">$section&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">20</span><span style="color: #007700">,&nbsp;</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>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
string(14) &quot;lle Bjori Ro&quot; 
</pre></div>
    </div>
   </div>
   <div class="example" id="example-2368">
    <p><strong>Example #4 Using stream contexts</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">//&nbsp;Create&nbsp;a&nbsp;stream<br /></span><span style="color: #0000BB">$opts&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;</span><span style="color: #DD0000">'http'</span><span style="color: #007700">=&gt;array(<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'method'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"GET"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'header'</span><span style="color: #007700">=&gt;</span><span style="color: #DD0000">"Accept-language:&nbsp;en\r\n"&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"Cookie:&nbsp;foo=bar\r\n"<br />&nbsp;&nbsp;</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</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">//&nbsp;Open&nbsp;the&nbsp;file&nbsp;using&nbsp;the&nbsp;HTTP&nbsp;headers&nbsp;set&nbsp;above<br /></span><span style="color: #0000BB">$file&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">false</span><span style="color: #007700">,&nbsp;</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 changelog" id="refsect1-function.file-get-contents-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      
      <tr>
       <td>5.1.0</td>
       <td>
        Added the <em><code class="parameter">offset</code></em> and
        <em><code class="parameter">maxlen</code></em> parameters.
       </td>
      </tr>

      <tr>
       <td>5.0.0</td>
       <td>
        Added context support.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.file-get-contents-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">This function is
binary-safe.</span></p></blockquote>
  <div class="tip"><strong class="tip">Tip</strong><p class="simpara">A URL can be used as a
filename with this function if the <a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen wrappers</a> have been enabled.
See  <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> for more details on how to specify the
filename. See the <a href="wrappers.html" class="xref">Supported Protocols and Wrappers</a> for links to information
about what abilities the various wrappers have, notes on their usage,
and information on any predefined variables they may
provide.</p></div>
  <div class="warning"><strong class="warning">Warning</strong><p class="para">When using SSL, Microsoft IIS
will violate the protocol by closing the connection without sending a
<em>close_notify</em> indicator. PHP will report this as &quot;SSL: Fatal
Protocol Error&quot; when you reach the end of the data. To work around this, the
value of <a href="errorfunc.configuration.html#ini.error-reporting" class="link">error_reporting</a> should be
lowered to a level that does not include warnings.
PHP 4.3.7 and higher can detect buggy IIS server software when you open
the stream using the <em>https://</em> wrapper and will suppress the
warning. When using  <span class="function"><a href="function.fsockopen.html" class="function">fsockopen()</a></span> to create an
<em>ssl://</em> socket, the developer is responsible for detecting
and suppressing this warning.</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.file-get-contents-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="function.file.html" class="function" rel="rdfs-seeAlso">file()</a> - Reads entire file into an array</span></li>
    <li class="member"> <span class="function"><a href="function.fgets.html" class="function" rel="rdfs-seeAlso">fgets()</a> - Gets line from file pointer</span></li>
    <li class="member"> <span class="function"><a href="function.fread.html" class="function" rel="rdfs-seeAlso">fread()</a> - Binary-safe file read</span></li>
    <li class="member"> <span class="function"><a href="function.readfile.html" class="function" rel="rdfs-seeAlso">readfile()</a> - Outputs a file</span></li>
    <li class="member"> <span class="function"><a href="function.file-put-contents.html" class="function" rel="rdfs-seeAlso">file_put_contents()</a> - Write a string to a file</span></li>
    <li class="member"> <span class="function"><a href="function.stream-get-contents.html" class="function" rel="rdfs-seeAlso">stream_get_contents()</a> - Reads remainder of a stream into a string</span></li>
    <li class="member"> <span class="function"><a href="function.stream-context-create.html" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - Creates a stream context</span></li>
    <li class="member"><a href="reserved.variables.httpresponseheader.html" class="link">$http_response_header</a></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.file-exists.html">file_exists</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.file-put-contents.html">file_put_contents</a></div>
 <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>