【发布时间】:2011-12-04 03:36:43
【问题描述】:
is_dir() 根据 php 手册,如果传递一个指向文件夹的字符串,则返回 true。 它返回错误——尽管传递了一组有效的文件夹名称——这可能很简单,但我因为找不到它而自责。
代码如下:
define('TEST_SUBFOLDERS_FILES_PATH', 'C:/xampp/htdocs/theProj/mainSubdir/');
$currentFolder = ' ';
$rootOfmainSubdir = TEST_SUBFOLDERS_FILES_PATH';
$theArrayOfDirsAndFilesInmainSubdir;
// fill an array that contains all files and folders under the /mainSubdir folder
// using php's 'scandir()'
if( ($theArrayOfDirsAndFilesInmainSubdir = scandir($rootOfmainSubdir )) != FALSE )
{
$numArrayEntries = count($theArrayOfDirsAndFilesInmainSubdir);
echo "<br><br>The number of folders/files found is: " . $numArrayEntries;
// now do the triage on subfolders under /mainSubdir versus files
for($i = 0; $i < $numArrayEntries; $i++)
{
echo "<br><br>Current array element is: "
. $theArrayOfDirsAndFilesInmainSubdir[$i]
. " and is_dir() on this element returns --> "
. var_dump( is_dir($theArrayOfDirsAndFilesInmainSubdir[$i]) );
// rest of code
这是我在几乎所有文件夹的输出中看到的:
Current array element is: testFolderN and is_dir() on this element returns -->
boolean false
或者我明白了:
Current array element is: . and is_dir() on this element returns -->
boolean true
与:
Current array element is: .. and is_dir() on this element returns -->
boolean true
我在这里错过了一些非常愚蠢的东西,但是太糟糕了。似乎 is_dir() 仅针对当前文件夹“。”正确返回 TRUE。和父文件夹“..”
如果我在使用 is_dir() 时遗漏了细微差别,我看不到它。
感谢您的帮助——我在代码中要做的就是列出 TEST_SUBFOLDERS_FILES_PATH 下的当前子文件夹,如果我能让 is_dir() 不仅为“.”返回 TRUE 的话。和“..”还有其他“testFolderN”子文件夹。
顺便说一下——目录里一共有5个子文件夹,1个文件,所以上面的count的显示是:
The number of folders/files found is: 8
【问题讨论】:
-
离题:- 可怕的变量名,太长且难以理解
-
代码太多,无法仅测试一个功能。
标签: php