Re: [PHP] DirectoryIterator

2010-08-02 Thread Nathan Nobbe
2010/8/2 João Cândido de Souza Neto 

> Is there a way of getting the symbolic link's target when using
> DirectoryIterator?
>

as DirectoryIterator traverses the contents of a directory, the current()
method will return an SplFileInfo instance.

Using the SplFileInfo, you can determine if the entry is a link and if so,
dereference it

http://www.php.net/manual/en/splfileinfo.islink.php

(note: $oFile is internally being populated via $oIt->current())

isLink())
  {
echo 'link found' . PHP_EOL;
echo 'link points to: ' . $oFile->getRealPath() . PHP_EOL;
  }
?>

-nathan


Re: [PHP] DirectoryIterator

2010-01-26 Thread 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



Re: [PHP] DirectoryIterator

2010-01-26 Thread Nathan Nobbe
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


Re: [PHP] DirectoryIterator

2010-01-26 Thread Nathan Nobbe
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


Re: [PHP] DirectoryIterator

2010-01-26 Thread Nathan Nobbe
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


Re: [PHP] DirectoryIterator

2010-01-26 Thread Christoph Boget
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.

getFilename() . '';
flush();
sleep( 1 );
  }
?>

thnx,
Christoph


Re: [PHP] DirectoryIterator

2010-01-26 Thread 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: [PHP] DirectoryIterator

2010-01-26 Thread Christoph Boget
>
> 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


Re: [PHP] DirectoryIterator

2010-01-26 Thread Nathan Nobbe
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:

isFile())
$this->_iCount++;
  }

  public function count()
  {
return $this->_iCount;
  }
?>

-nathan


Re: [PHP] DirectoryIterator

2010-01-26 Thread Ashley Sheridan
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




Re: [PHP] DirectoryIterator

2010-01-26 Thread Christoph Boget
>
> 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


Re: [PHP] DirectoryIterator

2010-01-26 Thread Nathan Nobbe
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