Files
php-doc-ru/reference/filesystem/functions/file.xml
2023-06-27 09:52:11 +03:00

193 lines
6.2 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="utf-8"?>
<!-- EN-Revision: 2957432ebc5ba2770805c08d5c867543ced8dd85 Maintainer: shein Status: ready -->
<!-- Reviewed: no -->
<refentry xmlns="http://docbook.org/ns/docbook" xml:id="function.file">
<refnamediv>
<refname>file</refname>
<refpurpose>Читает содержимое файла и помещает его в массив</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type class="union"><type>array</type><type>false</type></type><methodname>file</methodname>
<methodparam><type>string</type><parameter>filename</parameter></methodparam>
<methodparam choice="opt"><type>int</type><parameter>flags</parameter><initializer>0</initializer></methodparam>
<methodparam choice="opt"><type class="union"><type>resource</type><type>null</type></type><parameter>context</parameter><initializer>&null;</initializer></methodparam>
</methodsynopsis>
<para>
Читает содержимое файла и помещает его в массив.
</para>
<note>
<para>
Можно также использовать функцию <function>file_get_contents</function>
для получения файла в виде строки.
</para>
</note>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term><parameter>filename</parameter></term>
<listitem>
<para>
Путь к файлу.
</para>
&tip.fopen-wrapper;
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>flags</parameter></term>
<listitem>
<para>
В качестве необязательного параметра <parameter>flags</parameter> может
можно указать одну или более следующих констант:
<variablelist>
<varlistentry>
<term>
<constant>FILE_USE_INCLUDE_PATH</constant>
</term>
<listitem>
<simpara>
Ищет файл в <link linkend="ini.include-path">include_path</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<constant>FILE_IGNORE_NEW_LINES</constant>
</term>
<listitem>
<simpara>
Пропускать новую строку в конце каждого элемента массива
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<constant>FILE_SKIP_EMPTY_LINES</constant>
</term>
<listitem>
<simpara>
Пропускать пустые строки
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<constant>FILE_NO_DEFAULT_CONTEXT</constant>
</term>
<listitem>
<simpara>
Не использовать контекст по умолчанию
</simpara>
</listitem>
</varlistentry>
</variablelist>
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>context</parameter></term>
<listitem>
&note.context-support;
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Возвращает файл в виде массива. Каждый элемент массива соответствует
строке файла, с символами новой строки включительно. В случае
ошибки <function>file</function> возвращает &false;.
</para>
<note>
<para>
Каждая строка в полученном массиве будет завершаться символами конца
строки, если только не используется <constant>FILE_IGNORE_NEW_LINES</constant>).
</para>
</note>
&note.line-endings;
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
Вызывает ошибку уровня <constant>E_WARNING</constant>, если файл не существует.
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<para>
<example>
<title>Пример использования <function>file</function></title>
<programlisting role="php">
<![CDATA[
<?php
// Получает содержимое файла в виде массива. В данном примере мы используем
// обращение по протоколу HTTP для получения HTML-кода с удалённого сервера.
$lines = file('http://www.example.com/');
// Осуществим проход массива и выведем содержимое в виде HTML-кода вместе с номерами строк.
foreach ($lines as $line_num => $line) {
echo "Строка #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
// Используем необязательный параметр flags
$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
<refsect1 role="notes">
&reftitle.notes;
&warn.ssl-non-standard;
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><function>file_get_contents</function></member>
<member><function>readfile</function></member>
<member><function>fopen</function></member>
<member><function>fsockopen</function></member>
<member><function>popen</function></member>
<member><function>include</function></member>
<member><function>stream_context_create</function></member>
</simplelist>
</para>
</refsect1>
</refentry>
<!-- 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
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
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
-->