PHP下载方法挂页面...推送下载的最佳实践?

发布于 2024-09-24 23:55:49 字数 1136 浏览 2 评论 0原文

快速问题:我有一个表单强制用户输入电子邮件,然后推送下载/附件并下载文件...文件下载得很好...但是...

我的问题是,当下载开始,页面锁定,并且在文件下载完成之前用户无法导航到任何地方或在页面上执行任何操作(即:单击下面的“返回主页”链接)。还有比我在这里提出的更好的解决方案吗?我知道我可能错过了一些非常简单的东西......这是我第一次尝试设置私人下载页面。

<script type="text/javascript">
function redirect_function(loc){
    window.location = loc;
}
</script>

<?php
// after form is submitted
$condition_met=check($_POST['email']);

if($condition_met) { ?>
    <p>Your file will begin downloading in 5 seconds.</p> <a>go home</a>
    <script type="text/javascript">
        setTimeout('redirect_function("download.php")', 5000);
    </script>
<?php } ?>

被调用的(download.php)页面看起来像这样,这是它挂起页面的地方......

<?php
ob_start();
if($some_condition) { // check for authorization, etc
    $file='location/file.ext';
    header('Content-type: application/force-download');
    header('Content-disposition: attachment; filename="'. basename($file) .'"');
    header('Content-length: '. filesize($file) );
    readfile( $file );
} else {
    echo "error message";
}
ob_end_flush();
?>

quick question: I've got a form that forces a user to enter an email, after which a download/attachment is pushed and a file is downloaded... the file downloads fine... however...

My problem is that when the download starts, the page locks up, and the user can't navigate anywhere or do anything on the page until the file is downloaded (ie: clicking the "go home" link below). Any better solutions than what I come up with here? I know i'm probably missing something really simple ... this is my first crack at setting up a private download page.

<script type="text/javascript">
function redirect_function(loc){
    window.location = loc;
}
</script>

<?php
// after form is submitted
$condition_met=check($_POST['email']);

if($condition_met) { ?>
    <p>Your file will begin downloading in 5 seconds.</p> <a>go home</a>
    <script type="text/javascript">
        setTimeout('redirect_function("download.php")', 5000);
    </script>
<?php } ?>

The called (download.php) page looks like this, this is where it hangs up the page...

<?php
ob_start();
if($some_condition) { // check for authorization, etc
    $file='location/file.ext';
    header('Content-type: application/force-download');
    header('Content-disposition: attachment; filename="'. basename($file) .'"');
    header('Content-length: '. filesize($file) );
    readfile( $file );
} else {
    echo "error message";
}
ob_end_flush();
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

王权女流氓 2024-10-01 23:55:49

取出输出缓冲区。 Readfile() 将逐渐弹出数据,但您的输出缓冲区会捕获所有数据,直到它达到刷新状态。

Take the output buffer out. Readfile() will incrementally pop out the data as it goes, but your output buffer is catching it all until it gets to the flush.

冰葑 2024-10-01 23:55:49

差不多了,您需要将内容类型更改为

application/force-download

almost there, you need to change the content type to

application/force-download
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文