|
Posted by kenrbnsn on 07/03/06 08:11
dimo414 wrote (in part):
> <?php
>
> $dir = dir('subfolder/');
>
> while (false !== ($file = $dir->read()))
> {
> echo $file;
> echo ' ~ ';
> echo is_dir($file);
> echo '<br>';
> }
>
> ?>
>
> The output I receive is:
>
> . ~ 1
> .. ~ 1
> 001 ~
> 002 ~
> 003 ~
> 004 ~
>
> The files 001-004 are all folders, and yet they do not seem to pass
> is_dir().
The reason you're seeing this result is that the variable $file just
contains the name of the file, not the full path to the file, so the
is_dir() fuction is checking the see if that file is a directory in the
current directory, not the one you're probably expecting.
For your test script to work correctly, you need to prepend the path to
the directory onto the value in $file when using the is_dir() function.
Something like this:
<?php
$dir_str = 'subfolder/';
$dir = dir($dir_str);
while (false !== ($file = $dir->read()))
{
echo $file;
echo ' ~ ';
echo (is_dir($dir_str . $file))?'true':'false';
echo '<br>';
}
?>
You could use the glob() function instead of the dir() function to
return the files in the directory. The glob() function returns all the
files in an array. Each entry contains the full path, so your code
would look something like this:
<?php
$dir_str = 'subfolder/*'
$dir2 = glob($dir_str);
foreach($dir2 as $file) {
echo $file . ' ~ ';
echo (is_dir($file))?'true':'false';
echo '<br>';
}
?>
Ken
Navigation:
[Reply to this message]
|