我如何知道已发送了多少字节?
function Ifurl($subject)
{
$pattern = "/http:\/\//";
$regex = preg_match_all($pattern, $subject, $array);
if ($regex == 1)
{
return true; //true that it exist
}
else
{
return false;
}
}
function get_start_point($env_http_range)
{
//Calculate start point
$range = $env_http_range; // usually set to $_SERVER['HTTP_RANGE']
if ($range != '')
{
$p1 = strpos($range, "bytes=");
$p2 = strpos($range, "-");
$offset = substr($range, $p1+6, $p2-($p1+6));
} else {
$offset = 0;
}
return $offset;
}
function Output($url,$range,$range2) //cant record output 8192 not full amount
{
$opts = array('http'=>array('method'=>"GET",'header'=>"Range: bytes=$range-$range2"));
$context = stream_context_create($opts);
$fp = fopen($url, 'r', false, $context);
while(!feof($fp) && !connection_aborted())
{
$data_transferred += 1024 * 8;
fpassthru($fp);
}
$status = (!connection_aborted());
log_this($data_transferred);
fclose($httphandle);
}
function log_this($line)
{
$filename = 'download_log.txt';
$handle = fopen($filename, 'a');
fwrite($handle, $line . "\n");
}
function url($url)
{
$define_url = Ifurl($url); //define if the url is true!
if ($define_url == 1)
{
$filename = basename($url); //define the filename of the file
$httphandle = fopen($url, "r"); //open the file and read the information
$headers = stream_get_meta_data($httphandle);
$filesize = $headers['wrapper_data'][6]; //define the file conent size
$filecontent = $headers['wrapper_data'][8]; //get the file content
$pattern = "/(\d+)/"; //get all filesize dg
preg_match_all($pattern, $filesize, $array);
$filesize = $array[0][0]; //get the filesize;
$returnarray = array("Filename" => $filename,"Filesize" => $filesize, "Fileurl" => $url,"Filecontent" => $filecontent);
return $returnarray;
}
else
{
return "Unable"; //unable to make request!
}
}
function XD($url)
{
$graburl = url($url);
$filename = $graburl['Filename'];
$filesize = $graburl['Filesize'];
$filurl = $graburl['Fileurl'];
$filecontent = $graburl['Filecontent'];
///////////////////////////////////////////
set_magic_quotes_runtime(0);
ini_set('output_buffering', 'Off');
ob_end_clean();
ini_set('zlib.output_compression', 'Off');
ignore_user_abort(true);
////////////////////////////////////////////
@header("Cache-Control:");
@header("Cache-Control: public");
@header("Content-Type: application/octet-stream");
@header("Content-Disposition: attachment; filename=".$filename);
@header("Accept-Ranges: bytes");
$log = get_start_point($_SERVER['HTTP_RANGE']);
log_this($log);
if(isset($_SERVER['HTTP_RANGE']))
{
list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']);
list($range1,$range2) = explode("-", $range);
if ($range2 == '' or $range2 < 0 or $range2 >= $filesize) {
$range2 = $filesize -1;
};
if ($range1 < 0 or $range1 > $range2) {
$range1 = 0;
}
$new_length = $range2 - $range1 + 1;
@header("HTTP/1.1 206 Partial Content");
@header("Content-Range: bytes $range1-$range2/$filesize");
@header("Content-Length: $new_length");
Output($url,$range1,$range2);
}
else {
@header("Content-Length: ".$filesize);
$httphandle = fopen($url, "r"); //open the file and read the information
while (!feof($httphandle) && !connection_aborted())
{
$data_transferred += 1024 * 8;
echo fread($httphandle, 1024 * 8);
flush();
ob_end_flush();
}
$status = (!connection_aborted());
log_this($data_transferred);
fclose($httphandle);
}
}
XD('http://rarlabs.com/rar/wrar393.exe')
我正在尝试恢复下载并使用缓冲区记录已下载的字节,代码效果很好!但是我无法记录从用户下载的全部字节数,输出功能与服务器对话以发送用户停止的字节我用文本文件记录它作为测试,当用户下载时记录用户下载的字节并将其保存到文本文件中,但是唯一的问题是当用户调用函数输出时,日志不会记录我尝试使用 fread 的字节数,但代码会进入 choas 我如何记录用户的总字节数下载的字节数?
function Ifurl($subject)
{
$pattern = "/http:\/\//";
$regex = preg_match_all($pattern, $subject, $array);
if ($regex == 1)
{
return true; //true that it exist
}
else
{
return false;
}
}
function get_start_point($env_http_range)
{
//Calculate start point
$range = $env_http_range; // usually set to $_SERVER['HTTP_RANGE']
if ($range != '')
{
$p1 = strpos($range, "bytes=");
$p2 = strpos($range, "-");
$offset = substr($range, $p1+6, $p2-($p1+6));
} else {
$offset = 0;
}
return $offset;
}
function Output($url,$range,$range2) //cant record output 8192 not full amount
{
$opts = array('http'=>array('method'=>"GET",'header'=>"Range: bytes=$range-$range2"));
$context = stream_context_create($opts);
$fp = fopen($url, 'r', false, $context);
while(!feof($fp) && !connection_aborted())
{
$data_transferred += 1024 * 8;
fpassthru($fp);
}
$status = (!connection_aborted());
log_this($data_transferred);
fclose($httphandle);
}
function log_this($line)
{
$filename = 'download_log.txt';
$handle = fopen($filename, 'a');
fwrite($handle, $line . "\n");
}
function url($url)
{
$define_url = Ifurl($url); //define if the url is true!
if ($define_url == 1)
{
$filename = basename($url); //define the filename of the file
$httphandle = fopen($url, "r"); //open the file and read the information
$headers = stream_get_meta_data($httphandle);
$filesize = $headers['wrapper_data'][6]; //define the file conent size
$filecontent = $headers['wrapper_data'][8]; //get the file content
$pattern = "/(\d+)/"; //get all filesize dg
preg_match_all($pattern, $filesize, $array);
$filesize = $array[0][0]; //get the filesize;
$returnarray = array("Filename" => $filename,"Filesize" => $filesize, "Fileurl" => $url,"Filecontent" => $filecontent);
return $returnarray;
}
else
{
return "Unable"; //unable to make request!
}
}
function XD($url)
{
$graburl = url($url);
$filename = $graburl['Filename'];
$filesize = $graburl['Filesize'];
$filurl = $graburl['Fileurl'];
$filecontent = $graburl['Filecontent'];
///////////////////////////////////////////
set_magic_quotes_runtime(0);
ini_set('output_buffering', 'Off');
ob_end_clean();
ini_set('zlib.output_compression', 'Off');
ignore_user_abort(true);
////////////////////////////////////////////
@header("Cache-Control:");
@header("Cache-Control: public");
@header("Content-Type: application/octet-stream");
@header("Content-Disposition: attachment; filename=".$filename);
@header("Accept-Ranges: bytes");
$log = get_start_point($_SERVER['HTTP_RANGE']);
log_this($log);
if(isset($_SERVER['HTTP_RANGE']))
{
list($a, $range)=explode("=",$_SERVER['HTTP_RANGE']);
list($range1,$range2) = explode("-", $range);
if ($range2 == '' or $range2 < 0 or $range2 >= $filesize) {
$range2 = $filesize -1;
};
if ($range1 < 0 or $range1 > $range2) {
$range1 = 0;
}
$new_length = $range2 - $range1 + 1;
@header("HTTP/1.1 206 Partial Content");
@header("Content-Range: bytes $range1-$range2/$filesize");
@header("Content-Length: $new_length");
Output($url,$range1,$range2);
}
else {
@header("Content-Length: ".$filesize);
$httphandle = fopen($url, "r"); //open the file and read the information
while (!feof($httphandle) && !connection_aborted())
{
$data_transferred += 1024 * 8;
echo fread($httphandle, 1024 * 8);
flush();
ob_end_flush();
}
$status = (!connection_aborted());
log_this($data_transferred);
fclose($httphandle);
}
}
XD('http://rarlabs.com/rar/wrar393.exe')
I am trying to resume the download and record the bytes that has been downloaded using a buffer the code works great! But I am unable to record full amount of bytes downloaded from the user, the function Output talks to the server to send bytes that the user has left off I am recording it with a text file as a test, when the user downloads it logs the bytes that the user downloads an save it to a text file however the only problem is when the user calls the function output the log doesnt record how much bytes I try using fread but the code goes into choas how can I log the user for the total amount of bytes downloaded?
尝试在文档中查看此函数:
http://www.php .net/manual/en/function.fread.php#84115
Try checking this function out in the docs:
http://www.php.net/manual/en/function.fread.php#84115