php - Delete file after download with response in Cakephp -
my system creates temporary files , send them download. part works well. problem want delete files file system after user download file or after in point in time, seems afterfilter() function last controller method run, executes before file downloaded, not posibility or i'm missing something.
i have these functions in dowloadercontroller
public function download() { $filename = $this->session->read('namefile'); if (!is_file(storagepath . $filename)) { return; } $this->response->file( storagepath . $filename ); $this->response->download($filename); return $this->response; }
and
public function afterfilter() { if ($this->session->check('namefile')) { if (is_file(storagepath . $this->session->read('namefile'))) { @unlink(storagepath . $this->session->read('namefile')); } $this->session->delete('namefile'); } }
in previous version of cake, used use like:
$this->viewclass = 'media'; $params = array( 'id' => $filename, 'name' => $filealias, 'download' => true, 'extension' => $extension, 'path' => $path ); $this->set($params);
and worked well, doesnt
is there way unlink temporary file after dowloaded?
or
what have done solve problem?
using cakephp 2.8
because there no way know when file download finished , not have free access server following comments, ended creating function delete files except last 5 sorted date modified (just in case of multiple downloads different users) when download called.
using in controller:
app::uses('folder', 'utility'); app::uses('file', 'utility');
protected function _cleartemporaryfiles() { $dir = new folder(storagepath); $files = $dir->find('.*', true); if (count($files) > 5 ) { foreach ($files $key => $file) { $file = new file($dir->pwd() . ds . $file); $files[$key] = array(); $files[$key]['name'] = $file->name() . '.' . $file->ext(); $files[$key]['lastupdate'] = $file->lastchange(); $file->close(); } $orderedfiles = set::sort($files, '{n}.lastupdate', 'asc'); $orderedfiles = array_splice($orderedfiles, 0, count($orderedfiles) - 5); foreach ($orderedfiles $archivo) { $archivo = new file($dir->pwd() . ds . $archivo['name']); $archivo->delete(); $file->close(); } } }
it not efficient solution, or have wanted use, somehow solve problem. concern possibility of downloads @ same time , rare case more 5 @ time.