|
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
2 | | -<!-- $Revision$ --> |
3 | | -<!-- EN-Revision: b0b19b66183b28cebc1ffbcf1e2b8763f9d9c7b3 Maintainer: dallas Status: ready --> |
| 2 | +<!-- EN-Revision: 5c7e9e1351240b5f9e0858cdeba8f754a366d1b7 Maintainer: dallas Status: ready --> |
4 | 3 | <!-- CREDITS: mowangjuanzi --> |
5 | 4 | <refentry xml:id="function.opendir" xmlns="http://docbook.org/ns/docbook"> |
6 | 5 | <refnamediv> |
|
15 | 14 | <methodparam><type>string</type><parameter>directory</parameter></methodparam> |
16 | 15 | <methodparam choice="opt"><type class="union"><type>resource</type><type>null</type></type><parameter>context</parameter><initializer>&null;</initializer></methodparam> |
17 | 16 | </methodsynopsis> |
18 | | - <para> |
| 17 | + <simpara> |
19 | 18 | 打开一个目录句柄,可用于之后的 |
20 | 19 | <function>closedir</function>,<function>readdir</function> 和 |
21 | 20 | <function>rewinddir</function> 调用中。 |
22 | | - </para> |
| 21 | + </simpara> |
23 | 22 | </refsect1> |
24 | 23 |
|
25 | 24 | <refsect1 role="parameters"> |
26 | 25 | &reftitle.parameters; |
27 | | - <para> |
28 | | - <variablelist> |
29 | | - <varlistentry> |
30 | | - <term><parameter>directory</parameter></term> |
31 | | - <listitem> |
32 | | - <para> |
33 | | - 要打开的目录路径 |
34 | | - </para> |
35 | | - </listitem> |
36 | | - </varlistentry> |
37 | | - <varlistentry> |
38 | | - <term><parameter>context</parameter></term> |
39 | | - <listitem> |
40 | | - <para> |
41 | | - <parameter>context</parameter> 参数的说明见手册中的 |
42 | | - <link linkend="ref.stream">Streams API</link> 一章。 |
43 | | - </para> |
44 | | - </listitem> |
45 | | - </varlistentry> |
46 | | - </variablelist> |
47 | | - </para> |
| 26 | + <variablelist> |
| 27 | + <varlistentry> |
| 28 | + <term><parameter>directory</parameter></term> |
| 29 | + <listitem> |
| 30 | + <simpara> |
| 31 | + 要打开的目录路径。 |
| 32 | + </simpara> |
| 33 | + </listitem> |
| 34 | + </varlistentry> |
| 35 | + <varlistentry> |
| 36 | + <term><parameter>context</parameter></term> |
| 37 | + <listitem> |
| 38 | + <simpara> |
| 39 | + <parameter>context</parameter> 参数的说明见手册中的 |
| 40 | + <link linkend="ref.stream">Streams API</link> 一章。 |
| 41 | + </simpara> |
| 42 | + </listitem> |
| 43 | + </varlistentry> |
| 44 | + </variablelist> |
48 | 45 | </refsect1> |
49 | 46 |
|
50 | 47 | <refsect1 role="returnvalues"> |
51 | 48 | &reftitle.returnvalues; |
52 | | - <para> |
53 | | - 如果成功则返回目录句柄的 <type>resource</type>,&return.falseforfailure;。 |
54 | | - </para> |
| 49 | + <simpara> |
| 50 | + 成功时返回目录句柄,&return.falseforfailure;。 |
| 51 | + </simpara> |
55 | 52 | </refsect1> |
56 | 53 |
|
57 | 54 | <refsect1 role="errors"> |
58 | 55 | &reftitle.errors; |
59 | 56 | &fs.emits.warning.on.failure; |
60 | | - <para> |
| 57 | + <simpara> |
61 | 58 | 如果 <parameter>directory</parameter> 不是有效的目录,由于权限不足或文件系统错误而无法打开目录,则可能会发生这种情况。 |
62 | | - </para> |
| 59 | + </simpara> |
63 | 60 | </refsect1> |
64 | 61 |
|
65 | 62 | <refsect1 role="changelog"> |
|
86 | 83 |
|
87 | 84 | <refsect1 role="examples"> |
88 | 85 | &reftitle.examples; |
89 | | - <para> |
90 | | - <example> |
91 | | - <title><function>opendir</function> 示例</title> |
92 | | - <programlisting role="php"> |
| 86 | + <example> |
| 87 | + <title> |
| 88 | + 列出目录中的所有条目,跳过特殊目录 <literal>.</literal> 和 <literal>..</literal> |
| 89 | + </title> |
| 90 | + <simpara> |
| 91 | + 由于文件和目录名称可能是 PHP 视为“假值”的字符串(例如名为 <literal>"0"</literal> |
| 92 | + 的目录),而 <function>readdir</function> 在读取完目录中所有条目后会返回 &false;,因此需使用 |
| 93 | + <literal>===</literal> <link linkend="language.operators.comparison">比较运算符</link>,才能准确区分名称为“假值”的目录条目与已读取完所有目录条目的情况。 |
| 94 | + </simpara> |
| 95 | + <programlisting role="php"> |
93 | 96 | <![CDATA[ |
94 | 97 | <?php |
95 | | -$dir = "/etc/php5/"; |
96 | 98 |
|
97 | | -// 打开已知目录,然后继续读取内容 |
98 | | -if (is_dir($dir)) { |
99 | | - if ($dh = opendir($dir)) { |
100 | | - while (($file = readdir($dh)) !== false) { |
101 | | - echo "filename: $file : filetype: " . filetype($dir . $file) . "\n"; |
| 99 | +if ($handle = opendir('/path/to/files')) { |
| 100 | + echo "Entries:\n"; |
| 101 | +
|
| 102 | + /* 正确处理可能会视为假值的目录条目 */ |
| 103 | + while (false !== ($entry = readdir($handle))) { |
| 104 | + if ($entry === '.' || $entry === '..') { |
| 105 | + continue; |
102 | 106 | } |
103 | | - closedir($dh); |
| 107 | + echo "$entry\n"; |
104 | 108 | } |
| 109 | +
|
| 110 | + closedir($handle); |
105 | 111 | } |
106 | 112 | ?> |
107 | 113 | ]]> |
108 | | - </programlisting> |
109 | | - &example.outputs.similar; |
110 | | - <screen> |
| 114 | + </programlisting> |
| 115 | + &example.outputs.similar; |
| 116 | + <screen> |
111 | 117 | <![CDATA[ |
112 | | -filename: . : filetype: dir |
113 | | -filename: .. : filetype: dir |
114 | | -filename: apache : filetype: dir |
115 | | -filename: cgi : filetype: dir |
116 | | -filename: cli : filetype: dir |
| 118 | +Entries: |
| 119 | +base |
| 120 | +en |
| 121 | +fr |
| 122 | +output.md |
| 123 | +test.php |
117 | 124 | ]]> |
118 | | - </screen> |
119 | | - </example> |
120 | | - </para> |
| 125 | + </screen> |
| 126 | + </example> |
121 | 127 | </refsect1> |
122 | 128 |
|
123 | 129 | <refsect1 role="seealso"> |
124 | 130 | &reftitle.seealso; |
125 | | - <para> |
126 | | - <simplelist> |
127 | | - <member><function>is_dir</function></member> |
128 | | - <member><function>readdir</function></member> |
129 | | - <member><function>dir</function></member> |
130 | | - </simplelist> |
131 | | - </para> |
| 131 | + <simplelist> |
| 132 | + <member><function>readdir</function></member> |
| 133 | + <member><function>rewinddir</function></member> |
| 134 | + <member><function>closedir</function></member> |
| 135 | + <member><function>dir</function></member> |
| 136 | + <member><function>is_dir</function></member> |
| 137 | + <member><function>glob</function></member> |
| 138 | + <member><function>scandir</function></member> |
| 139 | + </simplelist> |
132 | 140 | </refsect1> |
133 | 141 | </refentry> |
134 | 142 | <!-- Keep this comment at the end of the file |
|
0 commit comments