[nycphp-talk] Listing files... alphabetically
Adam Maccabee Trachtenberg
adam at trachtenberg.com
Tue Nov 25 16:25:52 EST 2003
On Tue, 25 Nov 2003, Robert Dumas wrote:
> Here's the code I have. I left in a few commented lines, but don't hold that
> against me.
Your code works for me. When you had the original opendir() or die()
line, did your code die? If so, it probably means the you don't have
permission to read the directory. (Which does seem weird because I'm
guessing its globally readable, but still.)
> <?php
> $path = '/usr/local/apache/cmjnetwork.com/htdocs/sirius/';
> //$handle = @opendir($path) or die("Unable to open $path");
> $handle = opendir($path);
>
> while ( false !== ($file = readdir($handle)) ) {
Try putting a 'print "$file\n";' line here to make sure you're
actually iterating through the files you think you're iterating through.
> The problem I get is that this error message when I try to view it:
> Warning: Wrong datatype in asort() call in
> /usr/local/apache/cmjnetwork.com/htdocs/sirius/index.php on line 35
This would happen if no files get placed in the $filetlist. The it's
not an array, but a string (or some other non-initialized value, like
NULL) and asort() will complain.
> Warning: Variable passed to each() is not an array or object in
> /usr/local/apache/cmjnetwork.com/htdocs/sirius/index.php on line 37
Likewise with each().
To prevent errors like this, always initialize variables. Placing a
simple:
$filetlist = array();
at the top of your code will prevent these errors even when there are
legitimately no files in your directory.
-adam
--
adam at trachtenberg.com
author of o'reilly's php cookbook
avoid the holiday rush, buy your copy today!
More information about the talk
mailing list