At the moment, what you are currently doing is one of the simplest ways
of doing it portably, provided that the stream is seekable.

The other way is to call php_stream_stat() which returns the size,
ctime, mtime etc. of the stream, and can theoretically work on virtual
filesystems and wrappers (if those things implement the stat op, that
is!).  This is perhaps a better solution because the HTTP wrapper could
(but does not yet) provide the Content-Length information in the size
field of the stat structure (of course, the remote server might not send
a Content-Length header, so no size information would be present).

At the end of the day, the correct solution for your code depends on
what your code is trying to do; not all streams are seekable, so the stat
option is better, but then, stat is not guaranteed to return a sensible
size (or even any information).

If you are working on the stream in read-only operation, you can open
the stream using the STREAM_MUST_SEEK flag; this will cause the streams
layer to determine if the stream is seeekable; if it is not, it will
copy the stream into a temporary stream which is seekable. (The temp
stream is implemented as a memory buffer when the data are below a
certain threshold; when it gets too big for memory, it will
automatically use a temporary file for storage).
So you will be guaranteed that seek (and stat) will work on the stream,
but not guaranteed to be working with the actual resource you requested.
(Hence the reason that it is not suitable for read/write mode).

I hope that has helped!

--Wez.

On Sat, 25 Jan 2003, l0t3k wrote:

> wez,
> what's the most portable way to get the size of a stream ? right now im
> using
>
> stream = php_stream_open_wrapper ( path,  mode,  options, opened);
> php_stream_seek ( stream, SEEK_END, SEEK_CUR);
> size = php_stream_tell(stream);
>
> note that i have to also process the files, so i rewind immediately
> afterwards.
>
> for the moment, this is ok since im currently only using file-based streams.
> but i'd like to be able to be as generic as possible.
>
> l0t3k
> ps. would it make sense to have a get_size stream op ?


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to