Tracking finished downloads in PHP
Tracking downloads is wonderful, but when you have large downloads like movies, people often cancel them before they finish. In order to track completed downloads properly, we have to run the download through PHP.
This class requires Cana_File and Cana_Table (optional).
First lets create our access class. This will work with our tracking database to log a started and finished download.
class downloadAccess extends Cana_Table { public function __construct($id = null) { $this ->table('luma_download') ->idVar('id_download') ->load($id); } }
Next we extend the file class. We will create an entry in the database at the beginning of the download, and then set the saved bytes after it is completed.
class myDownload extends Cana_File {
public function __construct($params) {
parent::__construct($params); // create the access object $this->_access = new downloadAccess; $this->_access->file = $params['file']; $this->_access->ip = $_SERVER['REMOTE_ADDR']; }
public function get($attachment = false, $resume = true) { // save the access object $this->_access->start = date('Y-m-d H:i:s'); $this->_access->save();
// give the file to the user parent::get($attachment, $resume);
// save the sent bytes $this->_access->bytes = $this->bytes; $this->_access->bytes_file = $this->size; $this->_access->end = date('Y-m-d H:i:s'); $this->_access->save(); } }
Once our class is set up, we now need to use it.
// create the zip $download = new myDownload(array( 'file' => 'file.mp4', 'path' => 'movies' )); // get the file $download->get();