Php gets downloaded file size in real time

how to get the size of downloaded files using php
I want to make a real-time download progress. I want to return the downloaded file size to the front end

Php
Mar.09,2021

function filesize ()? Or configure the request header Content-Length?


<?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");
Menu