Server-side, get progress on sending file

烂漫一生 提交于 2019-12-12 04:35:37

问题


Basically, what I want to do is to check how much of a file my webserver has sent to a client, when the client is downloading one. Is this even possible? Does apache provide any module/extension that would help me accomplish my task?

I use a linux distro, apache2 and php5. Regards.


回答1:


Browser provides this functionality if file has correct "Content-length" header set. Why do you want to implement this in your page?




回答2:


Solved it.

I simply open the file with PHP that I want to send to the client.

$fh = fopen($filePath, 'r');

Then I calculate 60% of the filesize by writing

$fileSize = filesize($filePath);
$sizeFirst = floor(($fileSize / 100) * 60);

Now the $sizeFirst variable contains the length of the first 60% of the file, in a numeric value. To calculate the rest 40% I use:

$sizeLast = $fileSize - $sizeFirst;

Now I can write out the first 60%, do my action, and then write outh the rest 40%.

$dataFirst = fread($fh, $sizeFirst);
echo($dataDirst);

// Do my action here.

$dataSecond = fread($fh, $sizeSecond);
echo($dataSecond);
exit();

I need to set the header(); before writing out this, the Content-length, Content-type and Content-Disposition must be set in order to send a valid header and filecontent to the client.

Hope it helps someone.



来源:https://stackoverflow.com/questions/1789214/server-side-get-progress-on-sending-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!