1

I'm using CakePHP's Media view to force file downloads. My code is pretty much exactly like the example provided in the cookbook, which I'll paste here for your convenience:

<?php
class ExampleController extends AppController {
    public function download () {
        $this->viewClass = 'Media';
        // Download app/outside_webroot_dir/example.zip
        $params = array(
            'id'        => 'example.zip',
            'name'      => 'example',
            'download'  => true,
            'extension' => 'zip',
            'path'      => APP . 'outside_webroot_dir' . DS
        );
        $this->set($params);
    }
}

In the database, I have a field that keeps track of how many times the file was downloaded. I'm looking for a way to make sure that this number is as accurate as possible, so if a user's download gets cancelled or times out, the number does not increment. Is there some way for CakePHP's Media view to report that the download was, indeed, successful?

Nick
  • 8,049
  • 18
  • 63
  • 107

1 Answers1

2

Detecting when a file has finished downloading is no easy task. This is something that would be done on the client side with javascript, but browsers do not give you any hooks for that.

There is a pretty clever solution here (setting a cookie and then looking for it with javascript), but it only tells you when the download has started.

Community
  • 1
  • 1
Jason Galuten
  • 1,042
  • 2
  • 11
  • 20