|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-10-07 13:41 UTC] richard dot quadling at bandvulc dot co dot uk
Description:
------------
I'm using SPL RecursiveIteratorIterator, CachingRecursiveIterator and RecursiveDirectoryIterator to get ALL the files in a directory structure.
Some of the files are being identied as directories when using DirectoryIterator::getType() and comparing this to the result of filetype().
Reproduce code:
---------------
<?php
class class_DirectoryWalker extends RecursiveIteratorIterator
{
function __construct($path)
{
parent::__construct(new CachingRecursiveIterator(new RecursiveDirectoryIterator($path), CIT_CALL_TOSTRING | CIT_CATCH_GET_CHILD), 1);
}
function __call($func, $params)
{
return call_user_func_array(array($this->getSubIterator(), $func), $params);
}
}
$objDIR = new class_DirectoryWalker('M:/Datastore');
foreach($objDIR as $sKey => &$objValue)
{
echo filetype($sKey) . ' ' . $objValue->getType() . " $sKey\n";
}
?>
Expected result:
----------------
dir dir M:/Datastore/Bandvulc Department Desktops
dir dir M:/Datastore/Bandvulc Department Desktops/Accounts
file file M:/Datastore/Bandvulc Department Desktops/Accounts/Shortcut to ECDL.lnk
dir dir M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Bandvulc Tyre Contracts.lnk
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Shortcut to Salvesen Europe Contacts.lnk
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Shortcut to Sinead Williams on bandsbs.lnk
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Shortcut to Strike Rates 2005.lnk
dir dir M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Unused Desktop Shortcuts
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Unused Desktop Shortcuts/Access Accounts (btc v1).lnk
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Unused Desktop Shortcuts/Access Accounts (btc v2).lnk
Actual result:
--------------
dir dir M:/Datastore/Bandvulc Department Desktops
dir dir M:/Datastore/Bandvulc Department Desktops/Accounts
file dir M:/Datastore/Bandvulc Department Desktops/Accounts/Shortcut to ECDL.lnk
dir dir M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Bandvulc Tyre Contracts.lnk
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Shortcut to Salvesen Europe Contacts.lnk
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Shortcut to Sinead Williams on bandsbs.lnk
file dir M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Shortcut to Strike Rates 2005.lnk
dir dir M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Unused Desktop Shortcuts
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Unused Desktop Shortcuts/Access Accounts (btc v1).lnk
file file M:/Datastore/Bandvulc Department Desktops/Bandvulc Tyre Contracts/Unused Desktop Shortcuts/Access Accounts (btc v2).lnk
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 22:00:01 2025 UTC |
I altered the code just to show those that are different ... <?php class class_DirectoryWalker extends RecursiveIteratorIterator { function __construct($path) { parent::__construct(new CachingRecursiveIterator(new RecursiveDirectoryIterator($path), CIT_CALL_TOSTRING | CIT_CATCH_GET_CHILD), 1); } function __call($func, $params) { return call_user_func_array(array($this->getSubIterator(), $func), $params); } } $objDIR = new class_DirectoryWalker('M:/Datastore'); foreach($objDIR as $sKey => &$objValue) { if (filetype($sKey) != $objValue->getType()) { echo filetype($sKey) . ' ' . $objValue->getType() . " $sKey\n"; } } ?> and get things like ... dir file M:/Datastore/Bandvulc Department Desktops/Sales/bij1100 file dir M:/Datastore/Bandvulc Department Desktops/Sales/bij1100/win2k_xp/english/hpzvip09.dl_ Here we see a directory (filetype()) which the next line is a file within that directory and that line is being shown as a directory when in fact it is obviously a folder.Ok (I didn't understand the answer as I don't know why the caching should have any difference). I think I found my problem. <?php class class_DirectoryWalker extends RecursiveIteratorIterator { function __construct($path) { parent::__construct(new CachingRecursiveIterator(new RecursiveDirectoryIterator($path), CIT_CALL_TOSTRING | CIT_CATCH_GET_CHILD), 1); } function __call($func, $params) { return call_user_func_array(array($this->getSubIterator(), $func), $params); } } $objDIR = new class_DirectoryWalker('C:/test/'); $iTotal = 0; $iRight = 0; $iWrong = 0; foreach($objDIR as $sKey => $objValue) { ++$iTotal; echo $iTotal . ' ' . filetype($sKey) . ' ' . $objValue->getType() . ' ' . $sKey . ' ' . $objValue . "\n"; if (filetype($sKey) != $objValue->getType()) { ++$iWrong; } else { ++$iRight; } } echo "Total : $iTotal\nRight : $iRight\nWrong : $iWrong\n"; ?> The directory test contains 1 file. 1 file dir C:/test/file1.txt Object id #3 Total : 1 Right : 0 Wrong : 1 This implies that the foreach() mechanism returns the current item for a key but the next entry for the value. Which is very much NOT what I would expect. I would expect the key to be the filename (as it is) and the value to be the object representing the filename. The question that comes from this is how do I get the object which is the FIRST file? Richard.