dto5637 2011-09-29 15:02
浏览 47
已采纳

如何在成功完成下载后让PHP执行一个函数?

I have the feeling this should be quite basic, but I've failed to find a solution anywhere.

I'm working on an application that sends out an email to admins when a file is downloaded. Currently the email is sent out when a download begins, but I need to change this so that it only sends when the download is completed.

I suspect something like PHP's register_shutdown_function might be what I need, but according to the PHP manual shutdown functions are now called before the request is completed. Which leaves me a bit stumped.

For what it's worth, the function that currently handles downloads outputs them like this:

readfile($path);
exit;

The site in question is pretty large and complicated, so ideally I'd like to make as little change to the current code as possible. Any advice on how to solve this problem would be very much appreciated! Thanks.

  • 写回答

2条回答 默认 最新

  • douguyi3903 2011-09-29 15:05
    关注
    readfile($path);
    mail('admin@example.com', 'Download completed', 'download be done, mon');
    exit();
    

    The readfile call will block the script until the file's been loaded and output to the webserver. Note that there is no way for a PHP script to detect if the client has actually successfully downloaded the file. The file itself will be cached partially by the webserver, and PHP's involvement ends when the webserver's cache absorbs the entire file. That means the user can abort the download (or a network glitch kills it), yet you'll still get notified that a download was completed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图