使用php 如何獲取已下載文件的大小 想做個(gè)實(shí)時(shí)下載進(jìn)度 我想把已下載的文件大小返回給前端
4 回答

一只甜甜圈
TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
函數(shù) filesize() ?還是說(shuō)配置請(qǐng)求頭部 Content-Length ?

暮色呼如
TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
$dst_file = fopen(sys_get_temp_dir() . '/' . uniqid($time) . '.txt','w');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 20971520);
$flag=0;
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch ,$str) use (&$flag,$dst_file){
$len = strlen($str);
file_put_contents($dst_file,$str,FILE_APPEND);
return $len;
});
$output = curl_exec($ch);
fclose($dst_file);
curl_close($ch);
curl下載文件可以用上面的方法。
- 4 回答
- 0 關(guān)注
- 477 瀏覽
添加回答
舉報(bào)
0/150
提交
取消