鍍金池/ 問(wèn)答/PHP  網(wǎng)絡(luò)安全/ php 實(shí)時(shí)獲取已下載文件大小

php 實(shí)時(shí)獲取已下載文件大小

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

回答
編輯回答
念初

……沒(méi)懂,前端想拿下載進(jìn)度的話可以用Ajax???

2017年5月8日 10:36
編輯回答
玩控

函數(shù) filesize() ?還是說(shuō)配置請(qǐng)求頭部 Content-Length ?

2017年8月10日 22:46
編輯回答
忠妾
$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下載文件可以用上面的方法。

2018年9月16日 16:28
編輯回答
凝雅
<?php
$fp = fopen("http://zhangmenshiting.qianqian.com/data2/music/3423b48bd35de901b3512fc1f28cd2e3/567299874/56729985416560064.mp3?xcode=1910cd6f28d29cd260e85653b43f0cf4", "rb");
$dfp = fopen("a.mp3", "wb");
$downloaded = 0;
while (!feof($fp)) {
    $line = fread($fp, 4096);
    $size = strlen($line);
    $downloaded += $size;
    fwrite($dfp, $line, $size);
    printf("%.2fMB\r", $downloaded / 1024 / 1024);
}
fclose($fp);
fclose($dfp);
printf("\n");
2017年7月7日 12:25