views:

44

answers:

2

Hello fellow programmers.

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.

A: 

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

Eimantas
The reason I want to check how much of a file the user has downloaded is so I can execute another action if it has sent more than 60% of the file.
Tark
A: 

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.

Tark