PHP Readfile() number of bytes when user aborted
<p>I'm using a PHP script to stream a live video (i.e. a file which never ends) from a remote source. The output is viewed in VLC, not a web browser. I need to keep a count of the number of bytes transferred. Here is my code:</p>

<pre><code>&lt;?php

ignore_user_abort(true);
$stream = $_GET['stream'];

if($stream == "vid1")
{
    $count = readfile('http://127.0.0.1:8080/');
    logThis($count);
}

function logThis($c)
{
    $myFile = "bytecount.txt";
    $handle = fopen($myFile,'a');
    fwrite($handle,"Count: " . $c . "\n");
    fclose($handle);
}

?&gt;
</code></pre>

<p>However it appears that when the user presses the stop button, logThis() is never called, even though I've put in ignore_user_abort(true);</p>

<p>Any ideas on what I'm doing wrong?</p>

<p>Thanks</p>

<p><strong>Update2</strong>: I've changed my code as I shoudn't be using ignore_user_abort(true) as that would continue to download the file forever even after the client has gone. I've changed my code to this:</p>

<pre><code>&lt;?php

$count = 0;

function bye()
{
    //Create Dummy File with the filename of equal to count
}

register_shutdown_function('bye');
set_time_limit(0);
ignore_user_abort(false);

$stream = $_GET['stream'];

if($stream == "vid1")
{
    $GLOBALS['count'] = readfile('http://127.0.0.1:8080/');
    exit();
}
</code></pre>

<p>?></p>

<p>My problem now is that when the script is aborted (i.e. user presses stop), readfile won't return a value (i.e. count remains at 0). Any ideas on how I can fix this?</p>

<p>Thanks</p>
