这里有这样的代码,允许用户在我的服务器上下载文件:
$ch_2 = curl_init();
curl_setopt($ch_2, CURLOPT_USERAGENT, 'Mozilla/4.0');
curl_setopt($ch_2, CURLOPT_URL, $download_link);
curl_setopt($ch_2, CURLOPT_AUTOREFERER, true);
curl_setopt($ch_2, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch_2, CURLOPT_COOKIESESSION, false);
curl_setopt($ch_2, CURLOPT_FAILONERROR, true);
curl_setopt($ch_2, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch_2, CURLOPT_FORBID_REUSE, true);
curl_setopt($ch_2, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch_2, CURLOPT_NOSIGNAL, true);
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch_2, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch_2, CURLOPT_TIMEOUT, 86400);
curl_setopt($ch_2, CURLOPT_LOW_SPEED_LIMIT, 10240);
curl_setopt($ch_2, CURLOPT_LOW_SPEED_TIME, 60);
curl_setopt($ch_2, CURLOPT_MAX_RECV_SPEED_LARGE, 500);
curl_exec($ch_2);
$curl_errno_2 = curl_errno($ch_2);
curl_close($ch_2);这些文件(在$download_link中链接)非常大,大约1-3 GB。
--这段代码运行良好,但我的问题是,当用户断开或中止下载时,脚本在服务器收到完整文件($download_link)之前不会停止。
如果我设置了"ignore_user_abort(true)",那么脚本就会停止,但是是否可以在脚本中检查客户端断开连接或下载中止并处理此问题?例如,更新MySQL数据库条目或其他什么?
例如,我知道我可以使用"readfile()“或fopen来修改这段代码,但是我将(而且必须)使用这个cURL代码。
是否可以检查此用户断开连接或下载中止,因为我只允许用户在每个IP地址上建立一个下载连接?因此,如果他们中止下载,我就不能更新我的MySQL数据库来管理来自这个IP地址的下载连接。
发布于 2012-10-31 17:27:32
您可以注册一个关闭函数。这将在脚本完成、用户中止或调用exit()或die()时调用。http://php.net/manual/en/function.register-shutdown-function.php
例如,(来自php.net):
<?php
function shutdown()
{
// This is our shutdown function, in
// here we can do any last operations
// before the script is complete.
echo 'Script executed with success', PHP_EOL;
}
register_shutdown_function('shutdown');
?>这是你能得到的最接近的。
“因为我只允许用户在每个IP地址上建立一个下载连接?”
我建议你这样做:
https://stackoverflow.com/questions/13163385
复制相似问题