我如何知道已发送了多少字节?

发布于 09-08 17:35 字数 4165 浏览 11 评论 0原文

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心激荡2024-09-15 17:35:29

你研究过卷曲扩展吗?

Have you looked into the curl extension?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文