<?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 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.file.php',
    1 => 'file',
    2 => 'Reads entire file into an array',
  ),
  'up' => 
  array (
    0 => 'ref.filesystem.php',
    1 => 'Filesystem Functions',
  ),
  'prev' => 
  array (
    0 => 'function.fgetss.php',
    1 => 'fgetss',
  ),
  'next' => 
  array (
    0 => 'function.file-exists.php',
    1 => 'file_exists',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/filesystem/functions/file.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.file" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">file</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">file</span> &mdash; <span class="dc-title">Reads entire file into an array</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.file-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">function</span> <span class="methodname"><strong>file</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$filename</code></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span>, <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>): <span class="type"><span class="type"><a href="language.types.array.php" class="type array">array</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>

  <p class="para rdfs-comment">
   Reads an entire file into an array.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    You can use <span class="function"><a href="function.file-get-contents.php" class="function">file_get_contents()</a></span> to return the contents
    of a file as a string.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.file-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">filename</code></dt>
     <dd>
      <p class="para">
       Path to the file.
      </p>
      <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.php#ini.allow-url-fopen" class="link">fopen wrappers</a> have been enabled.
See <span class="function"><a href="function.fopen.php" class="function">fopen()</a></span> for more details on how to specify the
filename. See the <a href="wrappers.php" 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>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       The optional parameter <code class="parameter">flags</code> can be one, or
       more, of the following constants:
       <dl>
        
         <dt>
          <strong><code><a href="filesystem.constants.php#constant.file-use-include-path">FILE_USE_INCLUDE_PATH</a></code></strong>
         </dt>
         <dd>
          <p class="simpara">
           Search for the file in the <a href="ini.core.php#ini.include-path" class="link">include_path</a>.
          </p>
         </dd>
        
        
         <dt>
          <strong><code><a href="filesystem.constants.php#constant.file-ignore-new-lines">FILE_IGNORE_NEW_LINES</a></code></strong>
         </dt>
         <dd>
          <p class="simpara">
           Omit newline at the end of each array element.
          </p>
         </dd>
        
        
         <dt>
          <strong><code><a href="filesystem.constants.php#constant.file-skip-empty-lines">FILE_SKIP_EMPTY_LINES</a></code></strong>
         </dt>
         <dd>
          <p class="simpara">
           Skip empty lines.
          </p>
         </dd>
        
        
         <dt>
          <strong><code><a href="filesystem.constants.php#constant.file-no-default-context">FILE_NO_DEFAULT_CONTEXT</a></code></strong>
         </dt>
         <dd>
          <p class="simpara">
           Don&#039;t use the default context.
          </p>
         </dd>
        
       </dl>
      </p>
     </dd>
    
    
     <dt><code class="parameter">context</code></dt>
     <dd>
       <p class="simpara">A <a href="stream.contexts.php" class="link">context stream</a>
<span class="type"><a href="language.types.resource.php" class="type resource">resource</a></span>.</p>
     </dd>
    
   </dl>
  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.file-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the file in an array. Each element of the array corresponds to a
   line in the file, with the newline still attached. Upon failure,
   <span class="function"><strong>file()</strong></span> returns <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Each line in the resulting array will include the line ending, unless
    <strong><code><a href="filesystem.constants.php#constant.file-ignore-new-lines">FILE_IGNORE_NEW_LINES</a></code></strong> is used.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <p class="simpara">Prior to PHP 8.1.0, the
<a href="filesystem.configuration.php#ini.auto-detect-line-endings" class="link">auto_detect_line_endings</a>
run-time configuration option could be enabled to help PHP properly recognize
line endings when reading files created on Macintosh systems. This option has
been deprecated as of PHP 8.1.0. If necessary, handle <code class="literal">&quot;\r&quot;</code>
line breaks manually instead.</p></p></blockquote>
 </div>


 <div class="refsect1 errors" id="refsect1-function.file-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="simpara">
   As of PHP 8.3.0, throws a <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span>
   if <code class="parameter">flags</code> includes any invalid values, such
   as <strong><code><a href="filesystem.constants.php#constant.file-append">FILE_APPEND</a></code></strong>.
  </p>
  <p class="para">
   Emits an <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> level error if the file
   does not exist.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.file-changelog">
  <h3 class="title">Changelog</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>Version</th>
      <th>Description</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.3.0</td>
      <td>
       <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> is thrown for any
       invalid values of <code class="parameter">flags</code>.
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.file-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>file()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #FF8000">// Get a file into an array.  In this example we'll go through HTTP to get
// the HTML source of a URL.
</span><span style="color: #0000BB">$lines </span><span style="color: #007700">= </span><span style="color: #0000BB">file</span><span style="color: #007700">(</span><span style="color: #DD0000">'http://www.example.com/'</span><span style="color: #007700">);

</span><span style="color: #FF8000">// Loop through our array, show HTML source as HTML source; and line numbers too.
</span><span style="color: #007700">foreach (</span><span style="color: #0000BB">$lines </span><span style="color: #007700">as </span><span style="color: #0000BB">$line_num </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">$line</span><span style="color: #007700">) {
    echo </span><span style="color: #DD0000">"Line #&lt;b&gt;</span><span style="color: #007700">{</span><span style="color: #0000BB">$line_num</span><span style="color: #007700">}</span><span style="color: #DD0000">&lt;/b&gt; : " </span><span style="color: #007700">. </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #0000BB">$line</span><span style="color: #007700">) . </span><span style="color: #DD0000">"&lt;br /&gt;\n"</span><span style="color: #007700">;
}

</span><span style="color: #FF8000">// Using the optional flags parameter
</span><span style="color: #0000BB">$trimmed </span><span style="color: #007700">= </span><span style="color: #0000BB">file</span><span style="color: #007700">(</span><span style="color: #DD0000">'somefile.txt'</span><span style="color: #007700">, </span><span style="color: #0000BB">FILE_IGNORE_NEW_LINES </span><span style="color: #007700">| </span><span style="color: #0000BB">FILE_SKIP_EMPTY_LINES</span><span style="color: #007700">);
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
    </div>

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


 <div class="refsect1 notes" id="refsect1-function.file-notes">
  <h3 class="title">Notes</h3>
  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">When using SSL, Microsoft IIS
will violate the protocol by closing the connection without sending a
<code class="literal">close_notify</code> 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.php#ini.error-reporting" class="link">error_reporting</a> should be
lowered to a level that does not include warnings.
PHP can detect buggy IIS server software when you open
the stream using the <code class="literal">https://</code> wrapper and will suppress the
warning. When using <span class="function"><a href="function.fsockopen.php" class="function">fsockopen()</a></span> to create an
<code class="literal">ssl://</code> socket, the developer is responsible for detecting
and suppressing this warning.</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.file-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.file-get-contents.php" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - Reads entire file into a string</span></li>
    <li><span class="function"><a href="function.readfile.php" class="function" rel="rdfs-seeAlso">readfile()</a> - Outputs a file</span></li>
    <li><span class="function"><a href="function.fopen.php" class="function" rel="rdfs-seeAlso">fopen()</a> - Opens file or URL</span></li>
    <li><span class="function"><a href="function.fsockopen.php" class="function" rel="rdfs-seeAlso">fsockopen()</a> - Open Internet or Unix domain socket connection</span></li>
    <li><span class="function"><a href="function.popen.php" class="function" rel="rdfs-seeAlso">popen()</a> - Opens process file pointer</span></li>
    <li><span class="function"><a href="function.include.php" class="function" rel="rdfs-seeAlso">include</a> - include</span></li>
    <li><span class="function"><a href="function.stream-context-create.php" class="function" rel="rdfs-seeAlso">stream_context_create()</a> - Creates a stream context</span></li>
   </ul>
  </p>
 </div>


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