读取范围块与php分块

时间:2013-08-04 21:49:15

标签: javascript php curl

我有一个输入字段,我粘贴下载网址。 之后,我使用AJAX请求来获取fileinfos,例如headerinfo,content-length,mime type&如果我使用curl accept-ranges。

然后我开始连续循环xhr2请求,其范围为我的php文件。

http://www.example.com/chunks.php?url=http://url.com/someFile.ext&range=0-1024
http://www.example.com/chunks.php?url=http://url.com/someFile.ext&range=1024-2048
....

我也可以将其改为

http://www.example.com/chunks.php?url=http://url.com/someFile.ext&range=0-1024
http://www.example.com/chunks.php?url=http://url.com/someFile.ext&range=1025-2049
....

取决于我的脚本开始读取文件的位置。

我的第一个方法是使用cUrl&设置范围

<?php
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$_GET['url']);
curl_setopt($ch,CURLOPT_RANGE,$_GET['range']);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$result=curl_exec($ch);
curl_close($ch);
echo $result;
?>

效果很好但是如果范围块大于1mb,则使用ajax在客户端onprogress事件上没有动画。

我可以使用自定义CURLOPT_READFUNCTION ...但我不知道它是如何工作的...所以我改变了方法并使用了简单的fopen

<?php
$r=explode('-',$_GET['range']);//get (from to) ranges
$cc=($r[1]-$r[0]); //Calculate Client Chunk length
$sc=128; //Set the Server chunk length

$b=""; //Buffer
$bytes=0; //bytes read

$h=fopen($_GET['url'],"rb"); // open the url
fseek($h,$r[0]); // jump to the from pointer retrieved from links

while($bytes<$cc){ //while bytes read is smaller than my client chunk
 $sc=(($bytes+$sc)>$cc?($cc-$bytes):$sc); //prolley an error here 
 //if the server chunk + bytes read is bigger than the client chunk
 //then the server chunk is clinet chunk - bytes read
 $b=fread($h,$sc); // read the buffer
 $bytes+=strlen($b); //add the buffer length to bytes read  
 echo $b;// echo the buffer
 ob_flush(); // flush 
 flush(); // flush
}
fclose($h); //close
?>

现在这个工作......我在客户端得到了正确的动画,并且最终的大小是正确的指针应该没问题(0-1024,1024-2048)因为我使用fseek&amp;&amp;的fread。

但文件已损坏。

现在经过一些测试......这很慢。

更好的方法是cUrl CURLOPT_READFUNCTIONfsoket开启......

所以我想:

<?php
function $READ(){
 //here i need small chuncks of the response flushed. 
}
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$_GET['url']);
curl_setopt($ch,CURLOPT_RANGE,$_GET['range']);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_READFUNCTION,$READ);
$result=curl_exec($ch);
curl_close($ch);
echo $result;
?>

如果您有更好的解决方案,我会对使用javascript和php的所有内容持开放态度。

重点是创建一个带有简历的下载管理器,该文件将文件存储到window.webkitRequestFileSystem中,而不会填充浏览器的内存。

假设客户端有8mb的块,服务器块是256kb ..

然后将每个块的8mb附加到先前使用window.webkitRequestFileSystem

创建的文件中

每256kb我都有平均下载速度的更新,这样我就可以创建一个漂亮的动画。

服务器上的php只使用256kb ram,客户端浏览器可以每8mb清空一次垃圾收集(理论上)。

EDIT2

对于这段代码,我找到了一个解决方案:

代码允许您获取范围,例如:0-100 并得到这个100字节的输出!!

这允许您拥有一个具有连续完美无瑕PROGRESSBAR的AJAX脚本

<?php
function w($ch,$chunk){ 
 echo $chunk;
 ob_flush();
 flush();
 return strlen($chunk);
};
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$_GET['url']);
curl_setopt($ch,CURLOPT_RANGE,$_GET['range']);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch,CURLOPT_WRITEFUNCTION,w);
curl_exec($ch);
curl_close($ch);
?>

但我希望你们有更好的解决方案!感谢

1 个答案:

答案 0 :(得分:0)

我可以使用PHP curl的CURLOPT_WRITEFUNCTION callback setting。以下用于该curl选项的示例回调函数curl_write_flush会写入收到的每个块并将输出刷新到浏览器。

<?php

/**
 * CURLOPT_WRITEFUNCTION which flushes the output buffer and the SAPI buffer.
 *
 * @param resource $curl_handle
 * @param string   $chunk
 */
function curl_write_flush($curl_handle, $chunk)
{ 
    echo $chunk;

    ob_flush(); // flush output buffer (Output Control configuration specific)
    flush();    // flush output body (SAPI specific)

    return strlen($chunk); // tell Curl there was output (if any).
};

$curl_handle = curl_init($_GET['url']);
curl_setopt($curl_handle, CURLOPT_RANGE, $_GET['range']);
curl_setopt($curl_handle, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_WRITEFUNCTION, 'curl_write_flush');
curl_exec($curl_handle);
curl_close($curl_handle);

我尝试使用小文件和大文件,但效果很好,但无法设置自定义块大小。

下载流的速度与我的ISP相同。

如果你有什么更好的话,我愿意接受任何答案。