Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116301 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 45641 invoked from network); 30 Oct 2021 10:42:17 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 30 Oct 2021 10:42:17 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EEAF61804F7 for ; Sat, 30 Oct 2021 04:32:58 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,KHOP_HELO_FCRDNS, SPF_HELO_NONE,SPF_NONE,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS16276 94.23.0.0/16 X-Spam-Virus: No X-Envelope-From: Received: from processus.org (ns366368.ip-94-23-14.eu [94.23.14.201]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 30 Oct 2021 04:32:58 -0700 (PDT) Received: from authenticated-user (PRIMARY_HOSTNAME [PUBLIC_IP]) by processus.org (Postfix) with ESMTPA id B3FC55101324; Sat, 30 Oct 2021 11:32:55 +0000 (UTC) Message-ID: <81816232-e99e-0762-c6d5-de2df6d34513@processus.org> Date: Sat, 30 Oct 2021 13:32:54 +0200 MIME-Version: 1.0 Content-Language: en-US To: Hans Henrik Bergan , PHP internals References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Authentication-Results: processus.org; auth=pass smtp.auth=pierre-php@processus.org smtp.mailfrom=pierre-php@processus.org X-Spamd-Bar: / Subject: Re: [PHP-DEV] fpassthru should get an optional length argument From: pierre-php@processus.org (Pierre) Le 30/10/2021 à 11:39, Hans Henrik Bergan a écrit : > ref https://bugs.php.net/bug.php?id=39135 , > i was thinking about how to implement HTTP Range requests/HTTP 206 Partial > Content in userland php, > and ofc it's possible to implement using fread() loops, but it'd be easier > (and faster? presumably) to implement if fpassthru() natively supported a > $max_bytes=null argument, > > an example of where it would be useful: > > $h=tmpfile(); > fwrite($h,"123456789ABCDEF"); > // this is _NOT_ a proper http-range-parser! > $range = $_SERVER['HTTP_RANGE'] ?? "bytes=5-9"; > $range=explode("-",explode("=",$range,2)[1],2); > $start = (int)$range[0]; > $byte_count = (-$start) + ((int)$range[1]) + 1; // the range format is > weird.. "Range: bytes=5-5" means "seek to byte #5 and serve 1 byte", 5-6 > means "seek to byte #5 and serve 2 bytes"~ > fseek($h,$start,SEEK_SET); > http_response_code(206); // HTTP 206 Partial Content > //echo fread($h,$byte_count); // << this is not suitable for large files, > if 1GB bytes is requested, this will use 1GB ram... > fpassthru($h, $byte_count); > // should print 6789A > Hello, Can't you use fseek() and stream_copy_to_stream() to achieve what you're trying to do without consuming memory ? Regards, -- Pierre