You are here: Re: is_dir() Function Not Working « All PHP « IT news, forums, messages
Re: is_dir() Function Not Working

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация