2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 07:05:15 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.file">
|
2007-02-03 08:13:44 +00:00
|
|
|
<refnamediv>
|
|
|
|
<refname>file</refname>
|
|
|
|
<refpurpose>Reads entire file into an array</refpurpose>
|
|
|
|
</refnamediv>
|
2007-06-16 01:17:14 +00:00
|
|
|
|
|
|
|
<refsect1 role="description">
|
|
|
|
&reftitle.description;
|
2007-02-03 08:13:44 +00:00
|
|
|
<methodsynopsis>
|
|
|
|
<type>array</type><methodname>file</methodname>
|
|
|
|
<methodparam><type>string</type><parameter>filename</parameter></methodparam>
|
2009-01-16 16:14:26 +00:00
|
|
|
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
|
2007-02-03 08:13:44 +00:00
|
|
|
<methodparam choice="opt"><type>resource</type><parameter>context</parameter></methodparam>
|
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
2007-06-16 01:17:14 +00:00
|
|
|
Reads an entire file into an array.
|
2007-02-03 08:13:44 +00:00
|
|
|
</para>
|
2007-06-16 01:17:14 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
You can use <function>file_get_contents</function> to return the contents
|
|
|
|
of a file as a string.
|
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
2007-02-03 08:13:44 +00:00
|
|
|
<para>
|
2007-04-03 20:38:29 +00:00
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2007-06-16 01:17:14 +00:00
|
|
|
<term><parameter>filename</parameter></term>
|
2007-04-03 20:38:29 +00:00
|
|
|
<listitem>
|
2007-06-16 01:17:14 +00:00
|
|
|
<para>
|
|
|
|
Path to the file.
|
|
|
|
</para>
|
|
|
|
&tip.fopen-wrapper;
|
2007-04-03 20:38:29 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
2007-06-16 01:17:14 +00:00
|
|
|
<term><parameter>flags</parameter></term>
|
2007-04-03 20:38:29 +00:00
|
|
|
<listitem>
|
2007-06-16 01:17:14 +00:00
|
|
|
<para>
|
|
|
|
The optional parameter <parameter>flags</parameter> can be one, or
|
|
|
|
more, of the following constants:
|
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<constant>FILE_USE_INCLUDE_PATH</constant>
|
|
|
|
</term>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
Search for the file in the <link
|
|
|
|
linkend="ini.include-path">include_path</link>.
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<constant>FILE_IGNORE_NEW_LINES</constant>
|
|
|
|
</term>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
Do not add newline at the end of each array element
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<constant>FILE_SKIP_EMPTY_LINES</constant>
|
|
|
|
</term>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
Skip empty lines
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
2010-04-07 17:09:10 +00:00
|
|
|
<!-- FIXME PHP_6
|
2007-08-18 04:37:18 +00:00
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<constant>FILE_TEXT</constant>
|
|
|
|
</term>
|
2007-08-18 09:01:06 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
The content is returned in UTF-8 encoding. You can specify a different
|
|
|
|
encoding by creating a custom context. This flag cannot be used with
|
2009-04-26 11:48:23 +00:00
|
|
|
<constant>FILE_BINARY</constant>.
|
2007-08-18 09:01:06 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
2007-08-18 04:37:18 +00:00
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term>
|
|
|
|
<constant>FILE_BINARY</constant>
|
|
|
|
</term>
|
2007-08-18 09:01:06 +00:00
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
The content is read as binary data. This is the default setting
|
2009-04-26 11:48:23 +00:00
|
|
|
and cannot be used with <constant>FILE_TEXT</constant>.
|
2007-08-18 09:01:06 +00:00
|
|
|
</simpara>
|
|
|
|
</listitem>
|
2007-08-18 04:37:18 +00:00
|
|
|
</varlistentry>
|
2010-04-07 17:09:10 +00:00
|
|
|
-->
|
2007-06-16 01:17:14 +00:00
|
|
|
</variablelist>
|
|
|
|
</para>
|
2007-04-03 20:38:29 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
2007-06-16 01:17:14 +00:00
|
|
|
<term><parameter>context</parameter></term>
|
2007-04-03 20:38:29 +00:00
|
|
|
<listitem>
|
2007-08-18 04:37:18 +00:00
|
|
|
<para>
|
|
|
|
A context resource created with the
|
|
|
|
<function>stream_context_create</function> function.
|
|
|
|
</para>
|
2007-06-16 01:17:14 +00:00
|
|
|
<para>
|
|
|
|
¬e.context-support;
|
|
|
|
</para>
|
2007-04-03 20:38:29 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
2007-02-03 08:13:44 +00:00
|
|
|
</para>
|
2007-06-16 01:17:14 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
|
|
|
<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,
|
|
|
|
<function>file</function> returns &false;.
|
|
|
|
</para>
|
2007-04-03 20:38:29 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
2007-06-16 01:17:14 +00:00
|
|
|
Each line in the resulting array will include the line ending, unless
|
|
|
|
<constant>FILE_IGNORE_NEW_LINES</constant> is used, so you still need to
|
|
|
|
use <function>rtrim</function> if you do not want the line ending
|
|
|
|
present.
|
2007-04-03 20:38:29 +00:00
|
|
|
</para>
|
|
|
|
</note>
|
2007-06-16 01:17:14 +00:00
|
|
|
¬e.line-endings;
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="changelog">
|
|
|
|
&reftitle.changelog;
|
|
|
|
<para>
|
|
|
|
<informaltable>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>&Version;</entry>
|
|
|
|
<entry>&Description;</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2010-04-07 17:09:10 +00:00
|
|
|
<!-- FIXME PHP_6
|
2007-08-18 04:37:18 +00:00
|
|
|
<row>
|
|
|
|
<entry>6.0.0</entry>
|
|
|
|
<entry>
|
|
|
|
Added support for the <constant>FILE_TEXT</constant> and
|
|
|
|
<constant>FILE_BINARY</constant> flags.
|
2009-04-26 11:48:23 +00:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
For <literal>forward compatibility</literal>, these constants are
|
|
|
|
available as of PHP 5.2.7
|
|
|
|
</para>
|
|
|
|
</note>
|
2007-08-18 04:37:18 +00:00
|
|
|
</entry>
|
|
|
|
</row>
|
2010-04-07 17:09:10 +00:00
|
|
|
-->
|
2007-06-16 01:17:14 +00:00
|
|
|
<row>
|
|
|
|
<entry>5.0.0</entry>
|
|
|
|
<entry>
|
|
|
|
The <parameter>context</parameter> parameter was added
|
|
|
|
</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>5.0.0</entry>
|
|
|
|
<entry>
|
|
|
|
Prior to PHP 5.0.0 the <parameter>flags</parameter> parameter only
|
|
|
|
covered <link linkend="ini.include-path">include_path</link> and was
|
|
|
|
enabled with 1
|
|
|
|
</entry>
|
|
|
|
</row>
|
|
|
|
<row>
|
|
|
|
<entry>4.3.0</entry>
|
|
|
|
<entry>
|
|
|
|
<function>file</function> became binary safe
|
|
|
|
</entry>
|
|
|
|
</row>
|
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</informaltable>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
2007-02-03 08:13:44 +00:00
|
|
|
<para>
|
2007-06-16 01:17:14 +00:00
|
|
|
<example>
|
|
|
|
<title><function>file</function> example</title>
|
2007-02-03 08:13:44 +00:00
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
2007-02-03 08:13:44 +00:00
|
|
|
// Get a file into an array. In this example we'll go through HTTP to get
|
2002-10-18 02:15:02 +00:00
|
|
|
// the HTML source of a URL.
|
2003-12-15 16:55:22 +00:00
|
|
|
$lines = file('http://www.example.com/');
|
2002-10-18 02:15:02 +00:00
|
|
|
|
2003-12-21 01:05:43 +00:00
|
|
|
// Loop through our array, show HTML source as HTML source; and line numbers too.
|
2002-10-18 02:15:02 +00:00
|
|
|
foreach ($lines as $line_num => $line) {
|
2003-12-15 16:55:22 +00:00
|
|
|
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
|
2002-04-15 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
2002-10-18 02:15:02 +00:00
|
|
|
// Another example, let's get a web page into a string. See also file_get_contents().
|
2003-12-15 16:55:22 +00:00
|
|
|
$html = implode('', file('http://www.example.com/'));
|
2008-02-29 17:47:09 +00:00
|
|
|
|
|
|
|
// Using the optional flags parameter since PHP 5
|
2008-02-29 17:55:20 +00:00
|
|
|
$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
|
2002-04-15 00:12:54 +00:00
|
|
|
?>
|
|
|
|
]]>
|
2007-02-03 08:13:44 +00:00
|
|
|
</programlisting>
|
2007-06-16 01:17:14 +00:00
|
|
|
</example>
|
2007-02-03 08:13:44 +00:00
|
|
|
</para>
|
2007-06-16 01:17:14 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="notes">
|
|
|
|
&reftitle.notes;
|
2007-02-03 08:13:44 +00:00
|
|
|
&warn.ssl-non-standard;
|
2007-06-16 01:17:14 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
2007-02-03 08:13:44 +00:00
|
|
|
<para>
|
2007-06-16 01:17:14 +00:00
|
|
|
<simplelist>
|
|
|
|
<member><function>readfile</function></member>
|
|
|
|
<member><function>fopen</function></member>
|
|
|
|
<member><function>fsockopen</function></member>
|
|
|
|
<member><function>popen</function></member>
|
|
|
|
<member><function>file_get_contents</function></member>
|
|
|
|
<member><function>include</function></member>
|
|
|
|
<member><function>stream_context_create</function></member>
|
|
|
|
</simplelist>
|
2007-02-03 08:13:44 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
2007-06-16 01:17:14 +00:00
|
|
|
|
2007-02-03 08:13:44 +00:00
|
|
|
</refentry>
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2002-04-15 00:12:54 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
|
|
|
-->
|