使用php readfile确定成功下载 [英] Determining successful download using php readfile

查看:84
本文介绍了使用php readfile确定成功下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要知道用户是否选择下载,然后单击取消按钮,这与readfile有错误。我已经检查了readfile函数返回的计数,但是即使用户从另存为对话框中取消了下载,它也显示文件中的字节。

I need to know if a user selected download then clicked the cancel button, which is not the same as readfile having an error. I have inspected the count returned by the readfile function, but it shows the bytes in the file even if the user canceled the download from the Save As dialog.

是因为我的网站有一次性下载,一个成员允许另一个用户下载他们的文件一次,然后权限消失。但是如果一个成员点击下载按钮,那么决定不下载它,那么我不想让我的数据库更新,以显示他们得到的文件。

The reason this is needed is because my site has a one-time download, where a member gives permission for another use to download their file one time, then the permission goes away. But if a member clicks the download button then decides not to download it right then, I dont' want my database to get updated to show they got the file.

具有知识产权保护,因为文件是上传成员的财产,我需要保留其他成员下载文件的审计跟踪,以防他们开始在互联网上浮动。但是如果readfile函数总是反映文件大小(意思是这些字节以某种方式传输),我无法知道文件是否实际下载。

This deals with intellectual property protection since the files are the property of the member who uploaded them, and I need to keep an audit trail of exactly what other members downloaded the file in case they start floating around the internet. But if the readfile function always reflects the filesize (meaning those bytes were transferred in some way), I have no way to know if the file was actually downloaded.

我有看到了一些关于这个问题的帖子,但没有真正的解决方案是什么是需要频繁的需求 - 他们是否下载?只要知道他们点击下载按钮并没有真正说明他们是否决定使用它,因为另存为对话框允许某人取消下载的实际完成。

I have seen a number of posts about this subject, but no real solutions to what has to be a frequent need - did they download it or not? Just knowing that they clicked the download button doesn't really say whether they decided to go through with it since the Save As dialog box allows someone to cancel the actual completion of the download.

为了完整起见,这里是我的下载代码,直到readfile函数为止:

For completeness, here is my download code up until the readfile function:

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Disposition: attachment; filename=$download_name");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize("sub/$doc_file"));
    ob_clean();
    flush();
    $wasdownloaded = readfile("sub/$doc_file");


推荐答案

您首先需要 ignore_user_abort()

将允许您的脚本在用户已取消或退出之后继续运行。

This would allow your script to continue on after the user has hit cancel, or escape.

然后您将需要 print 文件,并持续检查 connection_aborted()

You would then have to print out the file and continuously check with connection_aborted().

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename=$download_name");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize("sub/$doc_file"));
ob_clean();
flush();

$fp=fopen("sub/$doc_file","rb");

while(!feof($fp))
{
    print(fread($fp,1024*8));

    flush();
    ob_flush();
    if( connection_aborted() )
    {
        //do code for handling aborts
    }
}

这篇关于使用php readfile确定成功下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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