1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.35 $ -->
<reference id="ref.dir">
<title>Directory functions</title>
<titleabbrev>Directories</titleabbrev>
<partintro>
<simpara>
For related functions such as <function>dirname</function>,
<function>is_dir</function>, <function>mkdir</function>, and
<function>rmdir</function>, see the
<link linkend="ref.filesystem">Filesystem</link> section.
</simpara>
</partintro>
<refentry id="function.chroot">
<refnamediv>
<refname>chroot</refname>
<refpurpose>change the root directory</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>bool</type><methodname>chroot</methodname>
<methodparam><type>string</type><parameter>directory</parameter></methodparam>
</methodsynopsis>
<para>
Changes the root directory of the current process to
<parameter>directory</parameter>. Returns &false; if unable to
change the root directory, &true; otherwise.
</para>
<note>
<para>
It's not wise to use this function when running in a webserver
environment, because it's not possible to reset the root
directory to / again at the end of the request. This function
will only function correct when running as CGI this way.
</para>
</note>
</refsect1>
</refentry>
<refentry id="function.chdir">
<refnamediv>
<refname>chdir</refname>
<refpurpose>change directory</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>bool</type><methodname>chdir</methodname>
<methodparam><type>string</type><parameter>directory</parameter></methodparam>
</methodsynopsis>
<para>
Changes PHP's current directory to
<parameter>directory</parameter>. Returns &false; if unable to
change directory, &true; otherwise.
</para>
</refsect1>
</refentry>
<refentry id="class.dir">
<refnamediv>
<refname>dir</refname>
<refpurpose>directory class</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<classsynopsis>
<ooclass><classname>dir</classname></ooclass>
<constructorsynopsis>
<methodname>dir</methodname>
<methodparam><type>string</type><parameter>directory</parameter></methodparam>
</constructorsynopsis>
<fieldsynopsis>
<type>string</type><varname>path</varname>
</fieldsynopsis>
<methodsynopsis><type>string</type><methodname>read</methodname><void/></methodsynopsis>
<methodsynopsis><type>void</type><methodname>rewind</methodname><void/></methodsynopsis>
<methodsynopsis><type>void</type><methodname>close</methodname><void/></methodsynopsis>
</classsynopsis>
<para>
A pseudo-object oriented mechanism for reading a directory. The
given <parameter>directory</parameter> is opened. Two properties
are available once the directory has been opened. The handle
property can be used with other directory functions such as
<function>readdir</function>, <function>rewinddir</function> and
<function>closedir</function>. The path property is set to path
the directory that was opened. Three methods are available:
read, rewind and close.
</para>
<para>
Please note the fashion in which <function>dir</function>'s
return value is checked in the example below. We are explicitly
testing whether the return value is identical to (equal to and of
the same type as--see <link linkend="language.operators.comparison">
Comparison Operators</link> for more information) &false; since
otherwise, any directory entry whose name evaluates to &false; will
stop the loop.
<example>
<title><function>dir</function> example</title>
<programlisting role="php">
<![CDATA[
$d = dir("/etc");
echo "Handle: ".$d->handle."<br>\n";
echo "Path: ".$d->path."<br>\n";
while (false !== ($entry = $d->read())) {
echo $entry."<br>\n";
}
$d->close();
]]>
</programlisting>
</example>
</para>
<note>
<para>
The order in which directory entries are returned by the read method is
system-dependent.
</para>
</note>
</refsect1>
</refentry>
<refentry id="function.closedir">
<refnamediv>
<refname>closedir</refname>
<refpurpose>close directory handle</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>closedir</methodname>
<methodparam><type>resource</type><parameter>dir_handle</parameter></methodparam>
</methodsynopsis>
<para>
Closes the directory stream indicated by
<parameter>dir_handle</parameter>. The stream must have previously
been opened by <function>opendir</function>.
</para>
</refsect1>
</refentry>
<refentry id="function.getcwd">
<refnamediv>
<refname>getcwd</refname>
<refpurpose>gets the current working directory</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>string</type><methodname>getcwd</methodname>
<void/>
</methodsynopsis>
<para>
Returns the current working directory.
</para>
</refsect1>
</refentry>
<refentry id="function.opendir">
<refnamediv>
<refname>opendir</refname>
<refpurpose>open directory handle</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>resource</type><methodname>opendir</methodname>
<methodparam><type>string</type><parameter>path</parameter></methodparam>
</methodsynopsis>
<para>
Returns a directory handle to be used in subsequent
<function>closedir</function>, <function>readdir</function>, and
<function>rewinddir</function> calls.
</para>
<para>
If <parameter>path</parameter> is not a valid directory or the
directory can not be opened due to permission restrictions or
filesystem errors, <function>opendir</function> returns &false; and
generates a PHP error. You can suppress the error output of
<function>opendir</function> by prepending `@' to the front of
the function name.
</para>
<para>
<example>
<title><function>opendir</function> example</title>
<programlisting role="php">
<![CDATA[
<?php
if ($dir = @opendir("/tmp")) {
while (($file = readdir($dir)) !== false) {
echo "$file\n";
}
closedir($dir);
}
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
</refentry>
<refentry id="function.readdir">
<refnamediv>
<refname>readdir</refname>
<refpurpose>read entry from directory handle</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>string</type><methodname>readdir</methodname>
<methodparam><type>resource</type><parameter>dir_handle</parameter></methodparam>
</methodsynopsis>
<para>
Returns the filename of the next file from the directory. The
filenames are returned in the order in which they are stored by
the filesystem.
</para>
<para>
Please note the fashion in which <function>readdir</function>'s
return value is checked in the examples below. We are explicitly
testing whether the return value is identical to (equal to and of
the same type as--see <link
linkend="language.operators.comparison">Comparison
Operators</link> for more information) &false; since otherwise,
any directory entry whose name evaluates to &false; will stop the
loop.
</para>
<para>
<example>
<title>List all files in a directory</title>
<programlisting role="php">
<![CDATA[
// Note that !== did not exist until 4.0.0-RC2
<?php
$handle=opendir('/path/to/files');
echo "Directory handle: $handle\n";
echo "Files:\n";
/* This is the correct way to loop over the directory. */
while (false !== ($file = readdir($handle))) {
echo "$file\n";
}
/* This is the WRONG way to loop over the directory. */
while ($file = readdir($handle)) {
echo "$file\n";
}
closedir($handle);
?>
]]>
</programlisting>
</example>
</para>
<para>
Note that <function>readdir</function> will return the <literal>.</literal>
and
<literal>..</literal> entries. If you don't want these, simply strip
them out:
<example>
<title>
List all files in the current directory and strip out <literal>.</literal>
and <literal>..</literal>
</title>
<programlisting role="php">
<![CDATA[
<?php
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
?>
]]>
</programlisting>
</example>
</para>
</refsect1>
</refentry>
<refentry id="function.rewinddir">
<refnamediv>
<refname>rewinddir</refname>
<refpurpose>rewind directory handle</refpurpose>
</refnamediv>
<refsect1>
<title>Description</title>
<methodsynopsis>
<type>void</type><methodname>rewinddir</methodname>
<methodparam><type>resource</type><parameter>dir_handle</parameter></methodparam>
</methodsynopsis>
<para>
Resets the directory stream indicated by
<parameter>dir_handle</parameter> to the beginning of the
directory.
</para>
</refsect1>
</refentry>
</reference>
<!-- 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:"../../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
-->
|