<!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 an array</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.file-put-contents.html">file_put_contents</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fileatime.html">fileatime</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" class="refentry"> <div class="refnamediv"> <h1 class="refname">file</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">file</span> — <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="type">array</span> <span class="methodname"><strong>file</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$filename</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = 0</span></span> [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$context</code></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.html" 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> <span class="term"><em><code class="parameter">filename</code></em></span> <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.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> </dd> </dt> <dt> <span class="term"><em><code class="parameter">flags</code></em></span> <dd> <p class="para"> The optional parameter <em><code class="parameter">flags</code></em> can be one, or more, of the following constants: <dl> <dt> <span class="term"> <strong><code>FILE_USE_INCLUDE_PATH</code></strong> </span> <dd> <span class="simpara"> Search for the file in the <a href="ini.core.html#ini.include-path" class="link">include_path</a>. </span> </dd> </dt> <dt> <span class="term"> <strong><code>FILE_IGNORE_NEW_LINES</code></strong> </span> <dd> <span class="simpara"> Do not add newline at the end of each array element </span> </dd> </dt> <dt> <span class="term"> <strong><code>FILE_SKIP_EMPTY_LINES</code></strong> </span> <dd> <span class="simpara"> Skip empty lines </span> </dd> </dt> </dl> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">context</code></em></span> <dd> <p class="para"> A context resource created with the <span class="function"><a href="function.stream-context-create.html" class="function">stream_context_create()</a></span> function. </p> <p class="para"> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">Context support was added with PHP 5.0.0. For a description of <em>contexts</em>, refer to <a href="book.stream.html" class="xref">Streams</a>.</span></p></blockquote> </p> </dd> </dt> </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>FALSE</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>FILE_IGNORE_NEW_LINES</code></strong> is used, so you still need to use <span class="function"><a href="function.rtrim.html" class="function">rtrim()</a></span> if you do not want the line ending present. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the <a href="filesystem.configuration.html#ini.auto-detect-line-endings" class="link">auto_detect_line_endings</a> run-time configuration option may help resolve the problem.</span></p></blockquote> </div> <div class="refsect1 changelog" id="refsect1-function.file-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.0.0</td> <td> The <em><code class="parameter">context</code></em> parameter was added </td> </tr> <tr> <td>5.0.0</td> <td> Prior to PHP 5.0.0 the <em><code class="parameter">flags</code></em> parameter only covered <a href="ini.core.html#ini.include-path" class="link">include_path</a> and was enabled with 1 </td> </tr> <tr> <td>4.3.0</td> <td> <span class="function"><strong>file()</strong></span> became binary safe </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.file-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-2371"> <p><strong>Example #1 <span class="function"><strong>file()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #FF8000">// Get a file into an array. In this example we'll go through HTTP to get<br />// the HTML source of a URL.<br /></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">);<br /><br /></span><span style="color: #FF8000">// Loop through our array, show HTML source as HTML source; and line numbers too.<br /></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">=> </span><span style="color: #0000BB">$line</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Line #<b></span><span style="color: #007700">{</span><span style="color: #0000BB">$line_num</span><span style="color: #007700">}</span><span style="color: #DD0000"></b> : " </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">"<br />\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">// Another example, let's get a web page into a string. See also file_get_contents().<br /></span><span style="color: #0000BB">$html </span><span style="color: #007700">= </span><span style="color: #0000BB">implode</span><span style="color: #007700">(</span><span style="color: #DD0000">''</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">));<br /><br /></span><span style="color: #FF8000">// Using the optional flags parameter since PHP 5<br /></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">);<br /></span><span style="color: #0000BB">?></span> </span> </code></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="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 "SSL: Fatal Protocol Error" 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-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <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.fopen.html" class="function" rel="rdfs-seeAlso">fopen()</a> - Opens file or URL</span></li> <li class="member"> <span class="function"><a href="function.fsockopen.html" class="function" rel="rdfs-seeAlso">fsockopen()</a> - Open Internet or Unix domain socket connection</span></li> <li class="member"> <span class="function"><a href="function.popen.html" class="function" rel="rdfs-seeAlso">popen()</a> - Opens process file pointer</span></li> <li class="member"> <span class="function"><a href="function.file-get-contents.html" class="function" rel="rdfs-seeAlso">file_get_contents()</a> - Reads entire file into a string</span></li> <li class="member"> <span class="function"><a href="function.include.html" class="function" rel="rdfs-seeAlso">include</a> - include</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> </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-put-contents.html">file_put_contents</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.fileatime.html">fileatime</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>