我正在开发一个允许用户下载文件的系统,但是如果他们下载了一个文件,我想将此操作记录在一个特殊用途的表(MySQL)中。
我已经可以生成一个带有相应文件链接的图标,但是我看不到如何使单击图标下载文件的记录也创建日志记录。
我猜我将不得不使用一个按钮,并将按钮的操作设置为运行......什么?一个控制器动作,一个辅助函数,其他的……
这是我无法真正理解的最后一点。我将不胜感激任何可能实施过类似事情的人的建议!
体重
你有正确的想法。链接到控制器操作,该操作将写入数据库和日志,并将加载文件并将其呈现给用户。
例子:
class MyController extends AppController {
// Load the model
public $uses = ('DbTable');
public function get_file() {
// Save the DB record
$this->DbTable->save(...);
// Set the output header for content delivery
// (use the appropriate mime-type for your file)
header('Content-Type: image/jpg');
// Have it download as if it were an attachment
header('Content-Disposition: attachment; filename="filename.jpg"');
// Print out the file contents
echo file_get_contents('/path/to/filename.jpg');
// Prevent any further processing or rendering
exit();
}
}