Edit report at http://bugs.php.net/bug.php?id=52471&edit=1
ID: 52471 User updated by: d dot reade at ReadesGroupServices dot com Reported by: d dot reade at ReadesGroupServices dot com Summary: Folders being treated as files when scandir() is used Status: Bogus Type: Bug Package: Directory function related Operating System: CentOS 5.5 PHP Version: 5.3.3 Block user comment: N New Comment: I don't understand how it's my code? If you run the first example from the CWD, where "folderName" is located along with the test files/folders under "folderName", the results are that the test folders are actually files. The second example should be run from within the "folderName" folder, and this returns the correct results. The code in both examples is the same, the only difference is the folders we're scanning. Previous Comments: ------------------------------------------------------------------------ [2010-07-28 16:09:20] degeb...@php.net Yes, I did. The bug is in your code, not in scandir() nor is_dir(). ------------------------------------------------------------------------ [2010-07-28 16:07:23] d dot reade at ReadesGroupServices dot com Did you try the test script? ------------------------------------------------------------------------ [2010-07-28 16:03:09] degeb...@php.net It just returns a string. The output should tell you why it's failing. In Example 1, neither of those exist in the current directory, and things that don't exist are naturally not directories. ------------------------------------------------------------------------ [2010-07-28 15:33:03] d dot reade at ReadesGroupServices dot com Description: ------------ Using scandir() to fetch a list of folders/files in a folder. When I use foreach() and is_dir() to filter out folders from files, folders and files are being grouped together, almost like PHP thinks the folders are files. However if we use "." as the argument in the scandir() method, we get the desired results. The below test script assumes you've created the following folders/files: folderName/ folderName/testFolder1/ folderName/testFolder2/ folderName/testFile1 folderName/testFile2 These results occur using both the CLI and via a PHP page in the browser. Test script: --------------- <?php // Example 1 $scan = scandir("folderName"); foreach ($scan as $file) { if (is_dir($file)) { echo "Folder: ".$file."\n"; } else { echo "File: ".$file."\n"; } } ?> <?php // Example 2 $scan = scandir("."); foreach ($scan as $file) { if (is_dir($file)) { echo "Folder: ".$file."\n"; } else { echo "File: ".$file."\n"; } } ?> Expected result: ---------------- Example 2 gives the expected result: Folder: . Folder: .. File: testFile1 File: testFile2 Folder: testFolder1 Folder: testFolder2 Actual result: -------------- Example 1 gives the actual result: Folder: . Folder: .. File: testFile1 File: testFile2 File: testFolder1 File: testFolder2 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52471&edit=1