PHP 8.5.0 RC 2 available for testing

Voting

: max(six, three)?
(Example: nine)

The Note You're Voting On

mail at 3v1n0 dot net
17 years ago
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.

<?php
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;
}
?>

<< Back to user notes page

AltStyle によって変換されたページ (->オリジナル) /