开发者

Server-side, get progress on sending file

开发者 https://www.devze.com 2022-12-12 14:31 出处:网络
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?

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, apache开发者_StackOverflow2 and php5. Regards.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消