note 82841 added to function.fread

From: Date: Mon, 28 Apr 2008 22:32:18 +0000
Subject: note 82841 added to function.fread
Groups: php.notes 
Request: Send a blank email to php-notes+get-138884@lists.php.net to get a copy of this message
This is an hack I've done to download remote files with HTTP resume support. This is useful if you want to write a download script that fetches files remotely and then sends them to the user, adding support to download managers (I tested it on wget). To do that you should also use a "remote_filesize" function that you can easily write/find. <? function readfile_chunked_remote($filename, $seek = 0, $retbytes = true, $timeout = 3) { set_time_limit(0); $defaultchunksize = 1024*1024; $chunksize = $defaultchunksize; $buffer = ''; $cnt = 0; $remotereadfile = false; if (preg_match('/[a-zA-Z]+:\/\//', $filename)) $remotereadfile = true; $handle = @fopen($filename, 'rb'); if ($handle === false) { return false; } stream_set_timeout($handle, $timeout); if ($seek != 0 && !$remotereadfile) fseek($handle, $seek); while (!feof($handle)) { if ($remotereadfile && $seek != 0 && $cnt+$chunksize > $seek) $chunksize = $seek-$cnt; else $chunksize = $defaultchunksize; $buffer = @fread($handle, $chunksize); if ($retbytes || ($remotereadfile && $seek != 0)) { $cnt += strlen($buffer); } if (!$remotereadfile || ($remotereadfile && $cnt > $seek)) echo $buffer; ob_flush(); flush(); } $info = stream_get_meta_data($handle); $status = fclose($handle); if ($info['timed_out']) return false; if ($retbytes && $status) { return $cnt; } return $status; } ?> ---- Server IP: 80.241.173.254 Probable Submitter: 151.49.63.141 ---- Manual Page -- http://www.php.net/manual/en/function.fread.php Edit -- https://master.php.net/note/edit/82841 Del: integrated -- https://master.php.net/note/delete/82841/integrated Del: useless -- https://master.php.net/note/delete/82841/useless Del: bad code -- https://master.php.net/note/delete/82841/bad+code Del: spam -- https://master.php.net/note/delete/82841/spam Del: non-english -- https://master.php.net/note/delete/82841/non-english Del: in docs -- https://master.php.net/note/delete/82841/in+docs Del: other reasons-- https://master.php.net/note/delete/82841 Reject -- https://master.php.net/note/reject/82841 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138884) next »