Yes I am using PHP 5.0.4 and that was exactly the problem.
I am using Moodle 1.4.5 and there is no filelib.php in that version. So after googling I decided to patch PHP and recompile instead, according to the following diff:
--- php-5.0.4/main/php_streams.h.bug32553 2005-04-25 19:45:27.000000000 +0200(Source: https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=155916).
+++ php-5.0.4/main/php_streams.h 2005-04-25 19:46:33.000000000 +0200
@@ -413,7 +413,8 @@
* Uses mmap if the src is a plain file and at offset 0
* To ensure we don't take up too much memory when reading large files, set the default mmap length
* at this many bytes */
-#define PHP_STREAM_COPY_ALL 2000000
+// #define PHP_STREAM_COPY_ALL 2000000
+#define PHP_STREAM_COPY_ALL ((size_t)-1)
PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC);
It is all fixed now - I just wish is was easier to find out about this serious bug in PHP.