1

I have written code that creates a CSV file in PHP, and download it locally.

Is it possible to know whether the user has downloaded the file? Or if the user canceled downloading?

My test-code is placed below for creating and downloading the file:

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=invoies.csv");
header("Pragma: no-cache");
header("Expires: 0");

$array = array(
    array("data11", "data12", "data13"),
    array("data21", "data22", "data23"),
    array("data31", "data32", "data23"),
);
outputCSV($exported_invoices_arr);


function outputCSV($data) {
    $outstream = fopen("php://output", "w");

    function __outputCSV(&$vals, $key, $filehandler) {
        fputcsv($filehandler, $vals); // add parameters if you want
    }

    array_walk($data, "__outputCSV", $outstream);
    fclose($outstream);
}

Is their any trigger, or handler while downloading the file?

hakre
  • 193,403
  • 52
  • 435
  • 836
Suleman Ahmad
  • 2,025
  • 4
  • 28
  • 43
  • 3
    Not really but you might be interested in [Php connection_status](http://stackoverflow.com/questions/2389035/php-connection-status) / [PHP Connection Handling](http://www.php.net/features.connection-handling) – hakre Mar 13 '13 at 12:43
  • 1
    There's a lot of similar questions: http://stackoverflow.com/questions/2641667/deleting-a-file-after-user-download-it, http://stackoverflow.com/questions/7864407/php-limiting-parallel-simultaneous-downloads-how-to-know-if-download-was-canc, http://stackoverflow.com/q/1563187/46675, http://stackoverflow.com/questions/8771226/determining-successful-download-using-php-readfile, http://stackoverflow.com/questions/10162944/trigger-action-when-file-download-actually-completes – Mike B Mar 13 '13 at 12:48
  • Why not try a click logging function? Might not be able to track "who" downloads it, but it will keep track of the number of times it has been downloaded/clicked. – Funk Forty Niner Mar 13 '13 at 12:53
  • @Fred: Actually I have a constraint if user downloaded the file, then again he is not able to download it, so keeping track of downloads might not helpful – Suleman Ahmad Mar 13 '13 at 12:56
  • @hakre: similar technique/result is required, let I try it please – Suleman Ahmad Mar 13 '13 at 12:57
  • @john Ah ok. Will keep my thinking cap on. Cheers – Funk Forty Niner Mar 13 '13 at 12:58
  • 1
    @john: This has been covered in [a previous quesiton](http://stackoverflow.com/questions/2641667/deleting-a-file-after-user-download-it) - please search first. If anything you find on this website does not work for you then outline what you've tried so far and explain what does not work for you. Happy coding. – hakre Mar 13 '13 at 12:59
  • @hakre: I was searching this problem since 15-20 mints, But my bad not found any related link :( – Suleman Ahmad Mar 13 '13 at 13:02

2 Answers2

0

Its not possible .There is no straight forward method to capture. There might be some work around but its not perfect.

Samy
  • 632
  • 4
  • 14
0

You will never know. Even if you detect connection status, you're not sure, because a caching proxy could hide real user connection status. All what you know is if the script has sent all data, but not if file has been received.

You need to implement some client-side interactive downloader, like a java applet... but then you'll have other headaches.

Ghigo
  • 2,312
  • 1
  • 18
  • 19