下载特定文件URL时触发操作 [英] Trigger an action when a certain file URL is downloaded

查看:82
本文介绍了下载特定文件URL时触发操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Apache检测到某个文件URL已开始下载(或:已成功下载)时触发一个操作.

I would like to trigger an action when Apache detects that a certain file URL has been started for download (or: successfully downloaded).

示例: https://example.com/download/token_A6FZ523/myfile.zip 由客户端下载,对SQLite数据库执行以下查询:

Example: when https://example.com/download/token_A6FZ523/myfile.zip is downloaded by a client, execute the following query to a SQLite database:

INSERT INTO downloads(date, tokenID) VALUES(CURRENT_TIMESTAMP, "A6FZ523");

用法:然后,在PHP仪表板中,我可以检查谁下载了所提供的文件.

Usage: then, in a PHP Dashboard, I can check who has downloaded the delivered files.

我可以这样:

  • 每分钟在服务器上运行一个脚本,
  • 解析Apache日志/var/log/apache2/other_vhosts_access.log以搜索模式download/token_.*/myfile.zip

在这种情况下执行INSERT INTO查询

这似乎相当复杂,而且每分钟都必须运行脚本的事实并不是一个很好的解决方案.

This seems rather complex and the fact of having to run the script every minute is not a nice solution.

要求Apache将信息与客户端下载令牌A6FZ523关联的文件已下载到SQLite"是一个很好的解决方案吗?? 还是应该改用PHP?

What is a good solution to ask Apache to save to a SQLite database the information "The file associated to download token A6FZ523 has been downloaded by the client."? Or maybe should PHP be used instead?

推荐答案

我认为您的问题在于,您直接获取存储在服务器上的文件,而不是使用PHP以编程方式保留"该文件.这不是使用此方法遇到的第一个问题,您也无法检查安全性或从外部文件存储中获取文件(通常来说,这些天您不直接将文件存储在Web服务器上!).

I think your problem lies in that you are directly fetching a file that is stored on the server, as opposed to using PHP to "serve" this file programatically. This isn't the first problem you will encounter with this method, you also can't check for security or get the file from external file storage (generally speaking, you don't store files directly on the web server these days!).

但是,一旦您知道如何做,便很容易:)

But, simple to do once you know how :)

首先,让我们将下载文件的URL更改为 https://example .com/download.php?token = A6FZ523

Firstly, lets change the URL you download your file from to something like https://example.com/download.php?token=A6FZ523

因此,我们正在将GET变量发送到名为"download.php"的php脚本.在该脚本中,您将具有以下内容:

So, we are sending a GET variable to a php script named "download.php". In that script you will have something like the following:

<?php 
$token = $_GET['token'];

// Get the information about the file from the DB, something like:
// SELECT filename, size, path FROM files WHERE token = $token;
// Giving you $filename, $size and $path

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename); 
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size);

echo file_get_contents($path);

// This will be on a completed download
// INSERT INTO downloads(date, tokenID) VALUES(CURRENT_TIMESTAMP, $token);
?>

调用download.php文件时,将获取令牌并将其与数据库中文件的信息匹配.然后,您设置标题,该标题基本上告诉浏览器这是一个文件",您的浏览器通过正常执行文件下载来做出相应的响应.然后,您将文件的内容读取给用户.完成此操作后,您可以通过另一个数据库调用记录下载.

When the download.php file is called, the token is taken and matched to a file's info in the DB. You then set headers which basically tells the browser "this is a file", your browser responds accordingly by implementing a file download as normal. You then read the contents of the file to the user. Once this has been completed, you can log the download via another DB call.

要说的一个很大的事情是,该脚本(显然带有写入的DB调用)应该为您做最基础的事情,但是根据您的使用场景,还有很多要添加的内容.考虑安全性,输入验证,存储文件并发送MIME类型标头的位置.

A big thing to say is that this script (obviously with the DB calls written in) should do the very basics for you, but there is a lot more to add depending on your usage scenario. Think security, input validation, where you store your files and sending a MIME type header.

希望这可以使您指向正确的方向:)

Hopefully that should point you in the right direction though :)

这篇关于下载特定文件URL时触发操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆