From ba0ec0111a1773b55e9a69a44fa245a5620b9f96 Mon Sep 17 00:00:00 2001 From: jim winstead <jimw@php.net> Date: Tue, 9 Apr 2002 22:29:22 +0000 Subject: [PATCH] update examples to test result of opendir() before trying to call readdir(). git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@77443 c90b9560-bf6c-de11-be94-00142212c4b1 --- functions/dir.xml | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/functions/dir.xml b/functions/dir.xml index ae782c1ad2..4c89f0cce2 100644 --- a/functions/dir.xml +++ b/functions/dir.xml @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="iso-8859-1"?> -<!-- $Revision: 1.37 $ --> +<!-- $Revision: 1.38 $ --> <reference id="ref.dir"> <title>Directory functions</title> <titleabbrev>Directories</titleabbrev> @@ -248,21 +248,22 @@ if ($dir = @opendir("/tmp")) { <![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"; +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 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); } - -/* This is the WRONG way to loop over the directory. */ -while ($file = readdir($handle)) { - echo "$file\n"; -} - -closedir($handle); ?> ]]> </programlisting> @@ -281,13 +282,14 @@ closedir($handle); <programlisting role="php"> <![CDATA[ <?php -$handle = opendir('.'); -while (false !== ($file = readdir($handle))) { - if ($file != "." && $file != "..") { - echo "$file\n"; - } +if ($handle = opendir('.')) { + while (false !== ($file = readdir($handle))) { + if ($file != "." && $file != "..") { + echo "$file\n"; + } + } + closedir($handle); } -closedir($handle); ?> ]]> </programlisting>