php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52471 Folders being treated as files when scandir() is used
Submitted: 2010-07-28 15:33 UTC Modified: 2010-07-28 16:40 UTC
From: d dot reade at ReadesGroupServices dot com Assigned:
Status: Not a bug Package: Directory function related
PHP Version: 5.3.3 OS: CentOS 5.5
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: d dot reade at ReadesGroupServices dot com
New email:
PHP Version: OS:

 

 [2010-07-28 15:33 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-28 16:03 UTC] degeberg@php.net
-Status: Open +Status: Bogus
 [2010-07-28 16:03 UTC] degeberg@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 16:07 UTC] d dot reade at ReadesGroupServices dot com
Did you try the test script?
 [2010-07-28 16:09 UTC] degeberg@php.net
Yes, I did. The bug is in your code, not in scandir() nor is_dir().
 [2010-07-28 16:14 UTC] d dot reade at ReadesGroupServices dot com
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.
 [2010-07-28 16:17 UTC] degeberg@php.net
You are *never* checking if something is a file. You are only checking if it's a 
directory. You are making the conclusion that if something is not a directory, 
then it must be a file. This conclusion is *incorrect*. For instance, there is 
the possibility that it doesn't even exist, which is the case here.
 [2010-07-28 16:40 UTC] d dot reade at ReadesGroupServices dot com
Sorry I see what you're saying - my bad! :(
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC