DirectoryIterator
am 26.01.2010 21:17:19 von Christoph Boget
--00c09f93db3d0797f5047e16f9aa
Content-Type: text/plain; charset=ISO-8859-1
I've looked through the docs but was unable to find out if this is possible;
I hope it is. Is there a way that you get the size/length of the collection
to be iterated (e.g. the total number of files) without having to iterate
through at least once?
thnx,
Christoph
--00c09f93db3d0797f5047e16f9aa--
Re: DirectoryIterator
am 26.01.2010 21:21:45 von Nathan Nobbe
--00504502beb0e999ff047e1708b9
Content-Type: text/plain; charset=UTF-8
On Tue, Jan 26, 2010 at 1:17 PM, Christoph Boget wrote:
> I've looked through the docs but was unable to find out if this is
> possible;
> I hope it is. Is there a way that you get the size/length of the
> collection
> to be iterated (e.g. the total number of files) without having to iterate
> through at least once?
>
def not on DirectorIterator afaik, and furthermore, i dont think thats
supported at the shell / filesystem level even.
-nathan
--00504502beb0e999ff047e1708b9--
Re: DirectoryIterator
am 26.01.2010 21:24:54 von Ashley Sheridan
--=-F452CdFeteEidZwRSkqJ
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
On Tue, 2010-01-26 at 15:25 -0500, Christoph Boget wrote:
> >
> > def not on DirectorIterator afaik, and furthermore, i dont think thats
> > supported at the shell / filesystem level even.
>
>
> Well if the Iterator has the whole of the collection in order to be able to
> iterate over it, I would think that it should be able to return the size of
> that collection... :(
>
> thnx,
> Christoph
Surely by that point it's already iterated at least once to put that
collection together?
Thanks,
Ash
http://www.ashleysheridan.co.uk
--=-F452CdFeteEidZwRSkqJ--
Re: DirectoryIterator
am 26.01.2010 21:25:48 von Christoph Boget
--00c09f8519805de323047e171779
Content-Type: text/plain; charset=ISO-8859-1
>
> def not on DirectorIterator afaik, and furthermore, i dont think thats
> supported at the shell / filesystem level even.
Well if the Iterator has the whole of the collection in order to be able to
iterate over it, I would think that it should be able to return the size of
that collection... :(
thnx,
Christoph
--00c09f8519805de323047e171779--
Re: DirectoryIterator
am 26.01.2010 21:33:23 von Nathan Nobbe
--001636e0b9c98a2d5c047e1732ea
Content-Type: text/plain; charset=UTF-8
On Tue, Jan 26, 2010 at 1:25 PM, Christoph Boget wrote:
> >
> > def not on DirectorIterator afaik, and furthermore, i dont think thats
> > supported at the shell / filesystem level even.
>
>
> Well if the Iterator has the whole of the collection in order to be able to
> iterate over it, I would think that it should be able to return the size of
> that collection... :(
>
right but the collection is built during iteration. one thing you could do
if you wanted it to *appear* as though the count was there at construction
is to subclass and iterate once in the constructor.
something like this:
class SmartDirectoryIterator extends DirectoryIterator implements Countable
{
private $_iCount = 0;
public function __construct($sPath)
{
parent::__construct($sPath);
foreach($this as $oFile)
if($oFile->isFile())
$this->_iCount++;
}
public function count()
{
return $this->_iCount;
}
?>
-nathan
--001636e0b9c98a2d5c047e1732ea--
Re: DirectoryIterator
am 26.01.2010 21:40:04 von Christoph Boget
--00c09f9c978f6430e8047e174a49
Content-Type: text/plain; charset=ISO-8859-1
>
> right but the collection is built during iteration.
>
So you're saying that if I add a file to the directory between the time I
instantiate the DirectoryIterator and the time I'm finished iterating
through, that file could be picked up? Or is the instance only going to
contain a list of files in that directory at the time the object was
constructed?
thnx,
Christoph
--00c09f9c978f6430e8047e174a49--
Re: DirectoryIterator
am 26.01.2010 22:01:12 von Kim Madsen
Christoph Boget wrote on 26/01/2010 21:17:
> I've looked through the docs but was unable to find out if this is possible;
> I hope it is. Is there a way that you get the size/length of the collection
> to be iterated (e.g. the total number of files) without having to iterate
> through at least once?
On Linux with safe mode off you can call system("du -hcs THE_DIR") to
get the size of all files, no iterations needed.
Number of files could be a find command piped into wc -l, called from
system(), like find . -name "*" | wc -l (but that would count dirs aswell)
--
Kind regards
Kim Emax - masterminds.dk
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: DirectoryIterator
am 26.01.2010 22:05:05 von Christoph Boget
--00c09f9db080da47d4047e17a3f5
Content-Type: text/plain; charset=ISO-8859-1
I executed the following test script several times. Each time, in a
separate terminal window, I ran "touch bob.txt" after the script started
echoing out. After the script completed, I deleted bob.txt. During each
execution, not once did bob.txt show up in the output. This makes me
believe that the file collection that DirectoryIterator is going to work
with is set at instantiation. If that's the case, it makes little sense to
me that you are unable to get the size of that collection from the
DirectoryInterator object.
$dirList = new DirectoryIterator(MY_DIR);
foreach($dirList as $file )
{
echo $file->getFilename() . '
';
flush();
sleep( 1 );
}
?>
thnx,
Christoph
--00c09f9db080da47d4047e17a3f5--
Re: DirectoryIterator
am 26.01.2010 22:06:48 von Nathan Nobbe
--001636e0b9c902d94f047e17aa2d
Content-Type: text/plain; charset=UTF-8
On Tue, Jan 26, 2010 at 2:05 PM, Christoph Boget wrote:
> I executed the following test script several times. Each time, in a
> separate terminal window, I ran "touch bob.txt" after the script started
> echoing out. After the script completed, I deleted bob.txt. During each
> execution, not once did bob.txt show up in the output. This makes me
> believe that the file collection that DirectoryIterator is going to work
> with is set at instantiation. If that's the case, it makes little sense to
> me that you are unable to get the size of that collection from the
> DirectoryInterator object.
>
>
> $dirList = new DirectoryIterator(MY_DIR);
> foreach($dirList as $file )
> {
> echo $file->getFilename() . '
';
> flush();
> sleep( 1 );
> }
> ?>
>
in that case you would likely need to expose something extra from the C
layer.
-nathan
--001636e0b9c902d94f047e17aa2d--
Re: DirectoryIterator
am 26.01.2010 22:08:55 von Nathan Nobbe
--001636e0a72da211f4047e17b12f
Content-Type: text/plain; charset=UTF-8
On Tue, Jan 26, 2010 at 2:01 PM, Kim Madsen wrote:
> Christoph Boget wrote on 26/01/2010 21:17:
>
> I've looked through the docs but was unable to find out if this is
>> possible;
>> I hope it is. Is there a way that you get the size/length of the
>> collection
>> to be iterated (e.g. the total number of files) without having to iterate
>> through at least once?
>>
>
> On Linux with safe mode off you can call system("du -hcs THE_DIR") to get
> the size of all files, no iterations needed.
>
right, thats the size, not a count of them.
> Number of files could be a find command piped into wc -l, called from
> system(), like find . -name "*" | wc -l (but that would count dirs aswell)
>
right, so on linux, you need to iterate over the filesystem to get a count
(unless someone knows a magic command the os / shell provides which returns
this info in a single call).
-nathan
--001636e0a72da211f4047e17b12f--
Re: DirectoryIterator
am 26.01.2010 22:24:01 von Nathan Nobbe
--001636b2bcbb95ac8a047e17e7d7
Content-Type: text/plain; charset=UTF-8
On Tue, Jan 26, 2010 at 2:06 PM, Nathan Nobbe wrote:
> On Tue, Jan 26, 2010 at 2:05 PM, Christoph Boget wrote:
>
>> I executed the following test script several times. Each time, in a
>> separate terminal window, I ran "touch bob.txt" after the script started
>> echoing out. After the script completed, I deleted bob.txt. During each
>> execution, not once did bob.txt show up in the output. This makes me
>> believe that the file collection that DirectoryIterator is going to work
>> with is set at instantiation. If that's the case, it makes little sense
>> to
>> me that you are unable to get the size of that collection from the
>> DirectoryInterator object.
>>
>>
>> $dirList = new DirectoryIterator(MY_DIR);
>> foreach($dirList as $file )
>> {
>> echo $file->getFilename() . '
';
>> flush();
>> sleep( 1 );
>> }
>> ?>
>>
>
> in that case you would likely need to expose something extra from the C
> layer.
>
fwiw.., a quick peak at the code reveals the following:
.. in the DirectoryIterator constructor it looks like the directory is
iterated at the C level
/* {{{ proto void DirectoryIterator::__construct(string path)
Cronstructs a new dir iterator from a path. */
SPL_METHOD(DirectoryIterator, __construct)
{
/* .... */
spl_filesystem_dir_open(intern, path TSRMLS_CC);
/* .... */
}
/* }}} */
.. this iteration is implemented in some php stream code, but it looks like
some internal C structures are tracking the file count
PHPAPI int _php_stream_scandir(char *dirname, char **namelist[], int flags,
php_stream_context *context,
int (*compare) (const char **a, const char **b) TSRMLS_DC)
{
/* im pretty sure this is where the number of files is stored .. */
int nfiles = 0;
/* read rest of function in main/streams/streams.c */
}
-nathan
--001636b2bcbb95ac8a047e17e7d7--
Re: DirectoryIterator
am 26.01.2010 22:37:22 von Paul M Foster
On Tue, Jan 26, 2010 at 10:01:12PM +0100, Kim Madsen wrote:
> Christoph Boget wrote on 26/01/2010 21:17:
>> I've looked through the docs but was unable to find out if this is
> possible;
>> I hope it is. Is there a way that you get the size/length of the
> collection
>> to be iterated (e.g. the total number of files) without having to iterate
>> through at least once?
>
> On Linux with safe mode off you can call system("du -hcs THE_DIR") to
> get the size of all files, no iterations needed.
This assumes the "du" command is available on the host system. Check
first to make sure.
Paul
--
Paul M. Foster
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php