|
1 | 1 | <?xml version="1.0" encoding="utf-8"?> |
2 | | -<!-- $Revision$ --> |
3 | | -<!-- splitted from ./en/functions/dir.xml, last change in rev 1.2 --> |
4 | 2 | <refentry xml:id="function.opendir" xmlns="http://docbook.org/ns/docbook"> |
5 | 3 | <refnamediv> |
6 | 4 | <refname>opendir</refname> |
|
14 | 12 | <methodparam><type>string</type><parameter>directory</parameter></methodparam> |
15 | 13 | <methodparam choice="opt"><type class="union"><type>resource</type><type>null</type></type><parameter>context</parameter><initializer>&null;</initializer></methodparam> |
16 | 14 | </methodsynopsis> |
17 | | - <para> |
| 15 | + <simpara> |
18 | 16 | Opens up a directory handle to be used in subsequent |
19 | 17 | <function>closedir</function>, <function>readdir</function>, and |
20 | 18 | <function>rewinddir</function> calls. |
21 | | - </para> |
| 19 | + </simpara> |
22 | 20 | </refsect1> |
23 | 21 |
|
24 | 22 | <refsect1 role="parameters"> |
25 | 23 | &reftitle.parameters; |
26 | | - <para> |
27 | | - <variablelist> |
28 | | - <varlistentry> |
29 | | - <term><parameter>directory</parameter></term> |
30 | | - <listitem> |
31 | | - <para> |
32 | | - The directory path that is to be opened |
33 | | - </para> |
34 | | - </listitem> |
35 | | - </varlistentry> |
36 | | - <varlistentry> |
37 | | - <term><parameter>context</parameter></term> |
38 | | - <listitem> |
39 | | - <para> |
40 | | - For a description of the <parameter>context</parameter> parameter, |
41 | | - refer to <link linkend="ref.stream">the streams section</link> of |
42 | | - the manual. |
43 | | - </para> |
44 | | - </listitem> |
45 | | - </varlistentry> |
46 | | - </variablelist> |
47 | | - </para> |
| 24 | + <variablelist> |
| 25 | + <varlistentry> |
| 26 | + <term><parameter>directory</parameter></term> |
| 27 | + <listitem> |
| 28 | + <simpara> |
| 29 | + The directory path to open. |
| 30 | + </simpara> |
| 31 | + </listitem> |
| 32 | + </varlistentry> |
| 33 | + <varlistentry> |
| 34 | + <term><parameter>context</parameter></term> |
| 35 | + <listitem> |
| 36 | + <para> |
| 37 | + For a description of the <parameter>context</parameter> parameter, |
| 38 | + refer to <link linkend="ref.stream">the streams section</link> of |
| 39 | + the manual. |
| 40 | + </para> |
| 41 | + </listitem> |
| 42 | + </varlistentry> |
| 43 | + </variablelist> |
48 | 44 | </refsect1> |
49 | 45 |
|
50 | 46 | <refsect1 role="returnvalues"> |
51 | 47 | &reftitle.returnvalues; |
52 | | - <para> |
53 | | - Returns a directory handle <type>resource</type> on success, |
| 48 | + <simpara> |
| 49 | + Returns a directory handle on success, |
54 | 50 | &return.falseforfailure; |
55 | | - </para> |
| 51 | + </simpara> |
56 | 52 | </refsect1> |
57 | 53 |
|
58 | 54 | <refsect1 role="errors"> |
59 | 55 | &reftitle.errors; |
60 | 56 | &fs.emits.warning.on.failure; |
61 | | - <para> |
| 57 | + <simpara> |
62 | 58 | This may happen if <parameter>directory</parameter> is not a valid directory, |
63 | 59 | the directory can not be opened due to permission restrictions, |
64 | 60 | or due to filesystem errors. |
65 | | - </para> |
| 61 | + </simpara> |
66 | 62 | </refsect1> |
67 | 63 |
|
68 | 64 | <refsect1 role="changelog"> |
|
89 | 85 |
|
90 | 86 | <refsect1 role="examples"> |
91 | 87 | &reftitle.examples; |
92 | | - <para> |
93 | | - <example> |
94 | | - <title><function>opendir</function> example</title> |
95 | | - <programlisting role="php"> |
| 88 | + <example> |
| 89 | + <title> |
| 90 | + List all entries in a directory, skipping the special <literal>.</literal> |
| 91 | + and <literal>..</literal> directories |
| 92 | + </title> |
| 93 | + <simpara> |
| 94 | + Because file and directory names can be strings that PHP considers "falsy" |
| 95 | + (e.g. a directory named <literal>"0"</literal>) and |
| 96 | + <function>readdir</function> returns &false; when it has read all entries |
| 97 | + in a directory one needs to use the <literal>===</literal> |
| 98 | + <link linkend="language.operators.comparison">comparison operator</link> |
| 99 | + to properly distinguish between a directory entry whose name is "falsy" |
| 100 | + and having read all entries of the directory. |
| 101 | + </simpara> |
| 102 | + <programlisting role="php"> |
96 | 103 | <![CDATA[ |
97 | 104 | <?php |
98 | | -$dir = "/etc/php5/"; |
99 | 105 |
|
100 | | -// Open a known directory, and proceed to read its contents |
101 | | -if (is_dir($dir)) { |
102 | | - if ($dh = opendir($dir)) { |
103 | | - while (($file = readdir($dh)) !== false) { |
104 | | - echo "filename: $file : filetype: " . filetype($dir . $file) . "\n"; |
| 106 | +if ($handle = opendir('/path/to/files')) { |
| 107 | + echo "Directory handle: $handle\n"; |
| 108 | + echo "Entries:\n"; |
| 109 | +
|
| 110 | + /* Correctly handling directory entries that may be considered falsy */ |
| 111 | + while (false !== ($entry = readdir($handle))) { |
| 112 | + if ($entry === '.' || $entry === '..') { |
| 113 | + continue; |
105 | 114 | } |
106 | | - closedir($dh); |
| 115 | + echo "$entry\n"; |
107 | 116 | } |
| 117 | +
|
| 118 | + closedir($handle); |
108 | 119 | } |
109 | 120 | ?> |
110 | 121 | ]]> |
111 | | - </programlisting> |
112 | | - &example.outputs.similar; |
113 | | - <screen> |
| 122 | + </programlisting> |
| 123 | + &example.outputs.similar; |
| 124 | + <screen> |
114 | 125 | <![CDATA[ |
115 | 126 | filename: . : filetype: dir |
116 | 127 | filename: .. : filetype: dir |
117 | 128 | filename: apache : filetype: dir |
118 | 129 | filename: cgi : filetype: dir |
119 | 130 | filename: cli : filetype: dir |
120 | 131 | ]]> |
121 | | - </screen> |
122 | | - </example> |
123 | | - </para> |
| 132 | + </screen> |
| 133 | + </example> |
124 | 134 | </refsect1> |
125 | 135 |
|
126 | 136 | <refsect1 role="seealso"> |
127 | 137 | &reftitle.seealso; |
128 | | - <para> |
129 | | - <simplelist> |
130 | | - <member><function>is_dir</function></member> |
131 | | - <member><function>readdir</function></member> |
132 | | - <member><function>dir</function></member> |
133 | | - </simplelist> |
134 | | - </para> |
| 138 | + <simplelist> |
| 139 | + <member><function>readdir</function></member> |
| 140 | + <member><function>rewinddir</function></member> |
| 141 | + <member><function>closedir</function></member> |
| 142 | + <member><function>dir</function></member> |
| 143 | + <member><function>is_dir</function></member> |
| 144 | + <member><function>glob</function></member> |
| 145 | + <member><function>scandir</function></member> |
| 146 | + </simplelist> |
135 | 147 | </refsect1> |
136 | 148 | </refentry> |
137 | 149 | <!-- Keep this comment at the end of the file |
|
0 commit comments