php-doc-en/functions/dir.xml
Hartmut Holzgraefe 7839d91186 added DO NOT EDIT noctice to old english functions files,
removing the others


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@78562 c90b9560-bf6c-de11-be94-00142212c4b1
2002-04-17 19:58:46 +00:00

353 lines
11 KiB
XML

<!-- D O N O T E D I T T H I S F I L E ! ! !
it is still here for historical reasons only
(as translators may need to check old revision diffs)
if you want to change things documented in this file
you should now edit the files found under en/reference
instead -->
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.39 $ -->
<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>
<note>
<para>
This defines the internal class
<classname>Directory</classname>, meaning that you will not be
able to define your own classes with that name. For a full list
of predefined classes in PHP, please see <link
linkend="reserved.classes">Predefined Classes</link>.
</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>
<para>See also <function>is_dir</function>.</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
if ($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
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
]]>
</programlisting>
</example>
</para>
<para>See also <function>is_dir</function>.</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
-->